Ruby 使用(+)函数进行数组连接
Array#+()是一个数组类方法,它通过将两个数组合并到第三个数组来执行数组连接操作。
语法。Array.+()
参数。用于执行连接操作的数组。
返回通过合并两个数组得到的新数组。
例子 #1 :
# Ruby code for +() method
# showing concatenate operation
# declaring array
a = [18, 22, 33, 4, 5, 6]
# declaring array
b = [5, 4, 22, 1, 88, 9]
# declaring array
c = [18, 22, 33, 40, 50, 6]
# combining arrays
puts "combination of a and b : #{a + b}\n\n"
# combining arrays
puts "combination of a and c : #{a + c}\n\n"
# combining arrays
puts "combination of b and c : #{b + c}\n\n"
输出:
combination of a and b : [18, 22, 33, 4, 5, 6, 5, 4, 22, 1, 88, 9]
combination of a and c : [18, 22, 33, 4, 5, 6, 18, 22, 33, 40, 50, 6]
combination of b and c : [5, 4, 22, 1, 88, 9, 18, 22, 33, 40, 50, 6]
例子 #2 :
# Ruby code for +() method
# showing concatenate operation
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# combining arrays
puts "combination of a and b : #{a + b}\n\n"
# combining arrays
puts "combination of a and c : #{a + c}\n\n"
# combining arrays
puts "combination of b and c : #{b + c}\n\n"
输出:
combination of a and b : ["abc", "xyz", "dog", "cow", "cat", "dog"]
combination of a and c : ["abc", "xyz", "dog", "cat", "1", "dog"]
combination of b and c : ["cow", "cat", "dog", "cat", "1", "dog"]