Ruby-on-rails Rails 列表有 .first 和 .second —— 有 .hundredth 或 .sixty_nineth 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7730605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:08:55  来源:igfitidea点击:

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

ruby-on-railsrubyenumerable

提问by New Alexandria

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:

是否有 Rails 的类或其他扩展允许多于系列中的前几个元素(和最后一个)?这些工作:

[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 

回答by tadman

There was a time when Rails added these, but there was a lot of controversyso most were removed. The only one of this experiment that remains is Array#forty_two.

有一段时间 Rails 添加了这些,但有很多争议,所以大部分都被删除了。这个实验中唯一剩下的就是Array#forty_two

回答by Tim

You can just use square brackets:

您可以只使用方括号:

list[6]
list[100]

回答by Samnang

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:

在 activesupport 中,它确实将这些方法中的一些方法修补到 Array 类中。如果你真的想要更多,你可以看看如何从 activesupport 实现:

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/access.rb

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/access.rb