当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Array push()用法及代码示例


Array#push():push()是一个Array类方法,它将给定对象附加到此数组的末尾。

用法:Array.push()

参数:数组


返回:将给定对象附加到此数组的末尾。

示例1:

# Ruby code for push() method 
  
# declaring array 
a = [18, 22, 33, nil, 5, 6] 
  
# declaring array 
b = [1, 4, 1, 1, 88, 9] 
  
# declaring array 
c = [34, 5, 7] 
  
# push method example 
puts "push() method form : #{a.push(3)}\n\n"
  
puts "push() method form : #{b.push()}\n\n"
  
puts "push() method form : #{c.push(2)}\n\n"

输出:

push() method form : [18, 22, 33, nil, 5, 6, 3]

push() method form : [1, 4, 1, 1, 88, 9]

push() method form : [34, 5, 7, 2]

示例2:

# Ruby code for push() method 
  
# declaring array 
a = ["abc", "nil", "dog"] 
  
# declaring array 
c = [nil, "abc", "nil", "dog"] 
  
# declaring array 
b = ["cow", nil, "dog"] 
  
  
# push method example 
puts "push() method form : #{a.push(2)}\n\n"
  
puts "push() method form : #{b.push(1)}\n\n"
  
puts "push() method form : #{c.push(0)}\n\n"

输出:

push() method form : ["abc", "nil", "dog", 2]

push() method form : ["cow", nil, "dog", 1]

push() method form : [nil, "abc", "nil", "dog", 0]



相关用法


注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array push() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。