红宝石 |数组连接 (*) 方法
Array#*()是一个 Array 类方法,它对数组执行集合连接操作。并通过连接自身的 int 副本返回新数组。
Syntax: Array.*()
Parameter: Arrays for performing the join or concatenation operation.
Return: New arrays with concatenated int copies of self
示例 #1:
# Ruby code for *() method
# showing join operation
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# a concatenating b
puts "concatenation of a and b : #{a * "toy"}\n\n"
# a concatenating c
puts "concatenation of a and c : #{c * 1}\n\n"
# b concatenating c
puts "concatenation of b and c : #{b * "cat_rat"}\n\n"
输出 :
concatenation of a and b : abctoyxyztoydog
concatenation of a and c : ["cat", "1", "dog"]
concatenation of b and c : cowcat_ratcatcat_ratdog
示例 #2:
# Ruby code for *() method
# showing join operation
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# a concatenating b
puts "concatenation of a and b : #{a * 2}\n\n"
# a concatenating c
puts "concatenation of a and c : #{a * 1}\n\n"
# b concatenating c
puts "concatenation of b and c : #{b * "34"}\n\n"
# b concatenating c
puts "concatenation of b and c : #{c * "toy"}\n\n"
输出 :
concatenation of a and b : ["abc", "xyz", "dog", "abc", "xyz", "dog"]
concatenation of a and c : ["abc", "xyz", "dog"]
concatenation of b and c : cow34cat34dog
concatenation of b and c : cattoy1toydog