Ruby Array limit method

RubyArrays

Ruby Problem Overview


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

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

Thanks for your quick help!

Ruby Solutions


Solution 1 - Ruby

The Array#take method is probably what you want.

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

Solution 2 - Ruby

You have Array#first:

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

Solution 3 - Ruby

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

Just another way to do it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKieran KlaassenView Question on Stackoverflow
Solution 1 - RubyLarsenalView Answer on Stackoverflow
Solution 2 - RubyrubyprinceView Answer on Stackoverflow
Solution 3 - RubykojaktslView Answer on Stackoverflow