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

和自甴很熟 提交于 2019-11-30 00:13:27

问题


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 

回答1:


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.




回答2:


You can just use square brackets:

list[6]
list[100]



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!