R: How do you print the first 5 records in an array?

A

Andrea Forlin

@people[0..4].each { |item| puts item }

-----Messaggio originale-----
Da: (e-mail address removed) [mailto:[email protected]] Per conto di
Bob Sanders
Inviato: sabato 18 agosto 2007 11.55
A: ruby-talk ML
Oggetto: How do you print the first 5 records in an array?

Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?
 
E

Eric

Op za 18 aug 12:03:56 2007 CEST schreef Andrea Forlin in
de nieuwsgroep 'comp.lang.ruby':
@people[0..4].each { |item| puts item }

-----Messaggio originale-----
Da: (e-mail address removed) [mailto:[email protected]] Per conto di
Bob Sanders
Inviato: sabato 18 agosto 2007 11.55
A: ruby-talk ML
Oggetto: How do you print the first 5 records in an array?

Let's say I have an array defined as:

@people = ["Jim", "Jen", "Jess", "Jay", "Jack", "John", "Jeff", "Jed",
"Jill"]

How would I then print out the first 5 records of that array? (i.e. from
Jim to Jack)

Any ideas?

I'd do it like this:
puts @people[0..4]
Jim
Jen
Jess
Jay
Jack
=> nil

Is the .each construct better?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top