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

On Aug 18, 2007, at 09:38, Phlip wrote:

http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

puts @people[0…4].join(“\n”)

On Aug 18, 2007, at 4:55 AM, Bob S. wrote:

Let’s say I have an array defined as:

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

I see you got your answer, but I wanted to add that you can do the
above with a lot less noise:

@people = %w[Jim Jen Jess Jay Jack John Jeff Jed Jill]

James Edward G. II

Hi,

Am Samstag, 18. Aug 2007, 18:55:17 +0900 schrieb Bob S.:

How would I then print out the first 5 records of that array?

Depending on what you want to do further maybe this is worth
a consideration:

5.times { puts @people.shift }

Bertram

Hi –

On Sun, 19 Aug 2007, Bob G. wrote:

puts @people[0…4].join("\n")

Assuming people don’t end with “\n”, that will have the same effect as
doing it without the join:

irb(main):007:0> people = %w{ me you him her them }
=> [“me”, “you”, “him”, “her”, “them”]
irb(main):008:0> puts people[0…4].join("\n")
me
you
him
her
them
=> nil
irb(main):009:0> puts people[0…4]
me
you
him
her
them
=> nil

If they do end with “\n”, then they’ll be separated by blank lines if
you do the join.

David

Bertram S. schrieb:

Bertram

No need to iterate here, just
puts @people.slice!(0,5)

but personally I like
puts @people.first(5)
the most…

Regards
Florian

Bob S. wrote:

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?

5.times{|t| puts @people[t]}

Op zo 19 aug 01:17:56 2007 CEST schreef Florian Aßmann in
de nieuwsgroep ‘comp.lang.ruby’:

Bertram S. schrieb:

Hi,

Am Samstag, 18. Aug 2007, 18:55:17 +0900 schrieb Bob S.:

How would I then print out the first 5 records of that array?

but personally I like
puts @people.first(5)
the most…

@array.first(5) reads exactly like ‘the first 5 things’ from the
array…

Not without reason, i guess.

So, why do it another way?

Wow. I just learned something new. I didn’t know you could do that
with
#first . Thanks!
James

Robert K. wrote:

very readable

puts @people[0,5]
puts @people[0…5]
puts @people[0…4]
Then let’s at least use inject to create nice CPS-style solution:

puts_first_five = (0…5).inject(lambda {}) { |f, i| lambda { |a| f[a];
puts a[i] } }
puts_first_five[@people]