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
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]
[134, 374, 878, 98, 763, 47, 5669].first(5)
Needs a recent-ish Ruby to work.
Tor Erik
You can also do
a[0,5]
Anna Lissa