Ruby 数组限制方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7785420/
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-06 04:23:35  来源:igfitidea点击:

Ruby Array limit method

rubyarrays

提问by Kieran Klaassen

I want to limit an Array object. How is this possible with ruby

我想限制一个 Array 对象。这怎么可能用红宝石

['one','two','three'].limit(2) => ['one','two']

Thanks for your quick help!

感谢您的快速帮助!

回答by Larsenal

The Array#takemethod is probably what you want.

阵列#采取的方法可能是你想要的东西。

['one','two','three'].take(2)

回答by rubyprince

You have Array#first:

你有Array#first

['one','two','three'].first(2)
=> ['one', 'two']

回答by Shayne Wheeler

irb(main):001:0> [1,2,3,4,5].slice! 0,4
=> [1, 2, 3, 4]

Just another way to do it.

只是另一种方式来做到这一点。