Ruby 数组连接(*)方法

Ruby 数组连接(*)方法

Array#*() 是一个数组类方法,它对数组进行集合连接操作。并通过连接自身的int副本返回新的数组。

语法。Array.*()

参数。用于执行连接或串联操作的数组。

返回新的数组,其中有自己的int副本的连接。

例子 #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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程