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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。