Is there a class or other extension for Rails that allows more than the first few elements in a series (and the last)? These work:
[2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].fifth
# -> 53
[2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].last
# -> 34
so where is?
list.sixth
list.hundredth
There was a time when Rails added these, but there was a lot of controversy so most were removed. The only one of this experiment that remains is Array#forty_two
.
You can just use square brackets:
list[6]
list[100]
In activesupport, it does monkey patching few of these methods into Array class. If you really want more, you can take a look how to implement from activesupport:
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/access.rb
来源:https://stackoverflow.com/questions/7730605/rails-lists-have-first-and-second-is-there-a-hundredth-or-sixty-nineth