红宝石 |字符串 << 方法
<<()是 Ruby 中的 String 类方法,用于将给定对象附加到字符串。如果对象是一个整数,那么默认情况下它被认为是一个代码点并在被附加之前转换为一个字符。
Syntax: str << "object"
Parameters: Here, str is the specified string and object is appended to the string.
Returns: Appended string.
示例 1:
#ruby 2.3.1
# Ruby program to demonstrate
# the << method
# Taking a string and
# using the method
puts "Hey " << "Champ"
puts "Kirti " << 33
输出:
Hey Champ
Kirti !
示例 2:
#ruby 2.3.1
# Ruby program to demonstrate
# the << method
# Taking a string and
# using the method
puts "Ruby " << "String"
puts "Hello " << "Geeks!"
输出:
Ruby String
Hello Geeks!