Search code examples
ruby-on-railsrubyenumerable

Rails lists have .first and .second – is there a .hundredth or .sixty_nineth ?


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 

Solution

  • There was a time when Rails added these, but there was a lot of controversy so most were removed. The only remnant of this experiment is Array#forty_two:

    (1..100).to_a.forty_two
    # => 42