Kieran Klaassen
Kieran Klaassen

Reputation: 2202

Ruby Array limit method

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!

Upvotes: 54

Views: 42142

Answers (3)

kojaktsl
kojaktsl

Reputation: 122

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

Just another way to do it.

Upvotes: 8

Larsenal
Larsenal

Reputation: 51166

The Array#take method is probably what you want.

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

Upvotes: 115

rubyprince
rubyprince

Reputation: 17793

You have Array#first:

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

Upvotes: 41

Related Questions