红宝石 |字符串 * 方法
String# *是 Ruby 中的 String 类方法,用于返回包含接收者整数副本的新字符串。这里,整数必须大于或等于 0。
Syntax: str * Integer
Parameters: Here, str is the required string and integer is the number of copies.
Returns: This method returns the String containing integer copies of the receiver.
示例 1:
# Ruby program to demonstrate
# the * method
# Taking a string and
# using the method
puts "Ruby " * 4
puts "Hey " * 2
输出:
Ruby Ruby Ruby Ruby
Hey Hey
示例 2:
# Ruby program to demonstrate
# the * method
# Taking a string and
# using the method
puts "Hello " * 0
puts "Copy " * 5
输出:
Copy Copy Copy Copy Copy