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


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


Array#rassoc():rassoc()是一個Array類方法,它通過數組搜索元素。

用法: Array.rassoc()

參數: 
- Arrays for finding elements. 
- elements to detect

返回: Array after adding the elements at the end.

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

# Ruby code for rassoc() method 
   
# 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, 16] 
  
ar = [a, b, c] 
  
# searhing for an element 
puts "check:#{ar.rassoc(4)}\n\n"
  
puts "check:#{ar.rassoc(22)}\n\n"

輸出:


check:[5, 4, 22, 1, 88, 9]

check:[18, 22, 33, 4, 5, 6]

代碼2:rassoc()方法示例

# Ruby code for rassoc() method 
   
# declaring array 
a = ["abc", "xyz", "dog"] 
   
# declaring array 
b = ["cow", "cat", "dog"] 
   
# declaring array 
c = ["cat", "1", "dog"] 
  
ar = [a, b, c] 
  
  
# searhing for an element 
puts "check:#{ar.rassoc("xyz")}\n\n"
  
puts "check:#{ar.rassoc("1")}\n\n"

輸出:

check:["abc", "xyz", "dog"]

check:["cat", "1", "dog"]


相關用法


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