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


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


Array#concat():concat()是一個Array類方法,該方法將兩個數組附加在一起後返回該數組。

用法: Array.concat()

參數:  Arrays to be combined

返回: Append the two arrays

代碼#1:concat()方法示例

# Ruby code for concat() method 
# adding elements at the end 
  
# declaring array 
a = [18, 22, 33, nil, 5, 6] 
  
# declaring array 
b = [5, 4, nil, 1, 88, 9] 
  
# declaring array 
c = [18, 22, nil, 40, 50, 6] 
  
# COMBINING TWO ARRAYS 
puts "combining a and b:#{a.concat(b)}\n\n"
  
puts "combining c and b:#{b.concat(c)}\n\n"
  
puts "combining a and c:#{c.concat(a)}\n\n"

輸出:


combining a and b:[18, 22, 33, nil, 5, 6, 5, 4, nil, 1, 88, 9]

combining c and b:[5, 4, nil, 1, 88, 9, 18, 22, nil, 40, 50, 6]

combining a and c:[18, 22, nil, 40, 50, 6, 18, 22, 33, nil, 5, 6, 5, 4, nil, 1, 88, 9]

代碼2:concat()方法示例

# Ruby code for concat() method 
# adding elements at the end 
  
# declaring array 
a = ["abc", "xyz", "dog"] 
  
# declaring array 
b = ["cow", "cat", "dog"] 
  
# declaring array 
c = ["cat", "1", "dog"] 
  
# COMBINING TWO ARRAYS 
puts "combining a and b:#{a.concat(b)}\n\n"
  
puts "combining c and b:#{b.concat(c)}\n\n"
  
puts "combining a and c:#{c.concat(a)}\n\n"

輸出:

combining a and b:["abc", "xyz", "dog", "cow", "cat", "dog"]

combining c and b:["cow", "cat", "dog", "cat", "1", "dog"]

combining a and c:["cat", "1", "dog", "abc", "xyz", "dog", "cow", "cat", "dog"]


相關用法


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