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

前端 未结 3 1984
旧巷少年郎
旧巷少年郎 2021-01-03 19:16

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,         


        
相关标签:
3条回答
  • 2021-01-03 19:23

    You can just use square brackets:

    list[6]
    list[100]
    
    0 讨论(0)
  • 2021-01-03 19:34

    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.

    0 讨论(0)
  • 2021-01-03 19:41

    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

    0 讨论(0)
提交回复
热议问题