當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Array append()用法及代碼示例


Array#append()是一個Array類方法,在數組末尾添加元素。

用法:Array.append()

參數:–用於添加元素的數組。
–要添加的元素


返回:在元素末尾添加數組。

示例1:

# Ruby code for append() 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] 
   
# appending array or element at the end of the array 
puts "adding elements in a : #{a.append(b)}\n\n"
   
puts "adding elements in b : #{b.append("ratttt")}\n\n"
   
puts "adding elements in c : #{c.append(b)}\n\n"

輸出:

adding elements in a : [18, 22, 33, 4, 5, 6, [5, 4, 22, 1, 88, 9]]

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 append() method 
# adding elements at the end 
    
# declaring array 
a = ["abc", "xyz", "dog"] 
    
# declaring array 
b = ["cow", "cat", "dog"] 
    
# declaring array 
c = ["cat", "1", "dog"] 
   
# appending array or element at the end of the array 
puts "adding elements in a : #{a.append(b)}\n\n"
   
puts "adding elements in b : #{b.append("ratttt")}\n\n"
   
puts "adding elements in c : #{c.append(b)}\n\n"

輸出:

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

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

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


相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array append() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。