📌  相关文章
📜  红宝石 |数组 <<() 方法

📅  最后修改于: 2022-05-13 01:55:11.060000             🧑  作者: Mango

红宝石 |数组 <<() 方法

Array#<<() : <<()是一个 Array 类方法,它在数组末尾添加元素。

示例 #1:

# Ruby code for <<() method
# adding elements at the end
   
# 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]
  
# adding array elements at the
# end of the array
puts "adding elements in a : #{a << "45" << ["cat", "rtt"]}\n\n"
  
puts "adding elements in b : #{b << "ratttt"}\n\n"
  
puts "adding elements in c : #{c << b}\n\n"

输出 :

adding elements in a : [18, 22, 33, 4, 5, 6, "45", ["cat", "rtt"]]

adding elements in b : [5, 4, 22, 1, 88, 9, "ratttt"]

adding elements in c : [18, 22, 33, 40, 50, 6, [5, 4, 22, 1, 88, 9, "ratttt"]]

示例 #2:

# Ruby code for <<() method
# adding elements at the end
   
# declaring array
a = ["abc", "xyz", "dog"]
   
# declaring array
b = ["cow", "cat", "dog"]
   
# declaring array
c = ["cat", "1", "dog"]
  
# adding array elements at the
# end of the array
puts "adding elements in a : #{a << "45" << ["cat", "rtt"]}\n\n"
  
puts "adding elements in b : #{b << "ratttt"}\n\n"
  
puts "adding elements in c : #{c << b}\n\n"

输出 :

adding elements in a : ["abc", "xyz", "dog", "45", ["cat", "rtt"]]

adding elements in b : ["cow", "cat", "dog", "ratttt"]

adding elements in c : ["cat", "1", "dog", ["cow", "cat", "dog", "ratttt"]]