Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Array limit method

Tags:

arrays

ruby

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!

like image 501
Kieran Klaassen Avatar asked Oct 16 '11 15:10

Kieran Klaassen


People also ask

How to find max number in array Ruby?

The array. max() method in Ruby enables us to find the maximum value among elements of an array. It returns the element with the maximum value.

How to Get minimum value in array Ruby?

Use the min Method to Get Minimum Number in an Array in Ruby Calling min without an argument returns the minimum number ( element ) but returns the n-smallest numbers if argument "n" is passed to it.

How do you slice an array in Ruby?

slice() in Ruby? The array. slice() is a method in Ruby that is used to return a sub-array of an array. It does this either by giving the index of the element or by providing the index position and the range of elements to return.


2 Answers

The Array#take method is probably what you want.

['one','two','three'].take(2) 
like image 170
Larsenal Avatar answered Sep 19 '22 14:09

Larsenal


You have Array#first:

['one','two','three'].first(2) => ['one', 'two'] 
like image 25
rubyprince Avatar answered Sep 19 '22 14:09

rubyprince