Fetch first 5 elements from array

Hi,
Is there any ruby function to fetch first five elements from array

for eg:a=[134,374,878,98,763,47,5669]
i want to get only first five i.e 134,374,878,98,763

Greetings –
~$ irb

a=(1 … 10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a[0 … 4]
=> [1, 2, 3, 4, 5]

Hope that helps…

err, make that a[0 … 4] or a[0 … 3]

You can also do

a[0,5]

Anna Lissa

On Wed, Jul 16, 2008 at 3:29 AM, Tor Erik L. <

[134, 374, 878, 98, 763, 47, 5669].first(5) :slight_smile:

Needs a recent-ish Ruby to work.

Tor Erik

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs