Array#at():at()是一个Array类方法,它以特定的实参索引值返回元素。
用法: Array.at() 参数: - Arrays to search elements. - index to search 返回: Array element at a specific index value
代码#1:at()方法示例
# Ruby code for at() 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] 
  
# finding the array element at  
# given index 
  
puts "element:#{a.at(4)}\n\n"
  
puts "element:#{a.at(-1)}\n\n"
  
puts "element:#{b.at(-2)}\n\n"
  
puts "element:#{c.at(-4)}\n\n"输出:
element:5 element:6 element:88 element:33
代码2:at()方法示例
# Ruby code for at() method 
   
# declaring array 
a = ["abc", "xyz", "dog"] 
   
# declaring array 
b = ["cow", "cat", "dog"] 
   
# declaring array 
c = ["cat", "1", "dog"] 
  
# finding the array element at  
# given index 
puts "element:#{a.at(4)}\n\n"
  
puts "element:#{a.at(-1)}\n\n"
  
puts "element:#{b.at(-2)}\n\n"
  
puts "element:#{c.at(2)}\n\n"输出:
element: element:dog element:cat element:dog
相关用法
- Ruby Array dig()用法及代码示例
 - Ruby Array map!()用法及代码示例
 - Ruby Array any?()用法及代码示例
 - Ruby Array combination()用法及代码示例
 - Ruby Array compact()用法及代码示例
 - Ruby Array compact!()用法及代码示例
 - Ruby Array concat()用法及代码示例
 - Ruby Array count()用法及代码示例
 - Ruby Array clear()用法及代码示例
 - Ruby Array collect!()用法及代码示例
 - Ruby Array size()用法及代码示例
 - Ruby Array bsearch()用法及代码示例
 - Ruby Array bsearch_index()用法及代码示例
 - Ruby Array rassoc()用法及代码示例
 - Ruby Array collect()用法及代码示例
 
注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array at() operation。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
