Forum: Ruby How to do a for loop...and iterate a set number of times?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B26b88117c4c2b7f7d1b5e4aa74ecdf4?d=identicon&s=25 Dan No (dann)
on 2009-01-29 23:19
So painfully basic, but I'm just starting Ruby and am coming to it from
C/C++/Java, etc. and some of the syntax is unnatural to me, and yes,
I've tried Google. I have an array of things and I want to access the
first 10...how do I do that?

for thing in things #(how do I set the beginning and end of the loop?)
#do something
end
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-01-29 23:27
(Received via mailing list)
one solution would be:
for thing in things[0..9]
...
end
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-01-29 23:30
(Received via mailing list)
El Jueves, 29 de Enero de 2009, Dan No escribió:
> for thing in things #(how do I set the beginning and end of the loop?)
> #do something
> end

things.each {|thing|
  #do something
}
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-01-29 23:37
(Received via mailing list)
Dan No wrote:
> So painfully basic, but I'm just starting Ruby and am coming to it from
> C/C++/Java, etc. and some of the syntax is unnatural to me, and yes,
> I've tried Google. I have an array of things and I want to access the
> first 10...how do I do that?
>
> for thing in things #(how do I set the beginning and end of the loop?)
> #do something
> end

Usually collections define an each method. Each call into the block gets
the next element in the collection. You don't have to worry about the
beginning and end of the loop. The each method and its friends are
considered the most idiomatic.

ary.each {|element| ...}

Or if you just want the equivalent of a C for loop, use upto. The block
argument is the current count, starting with 'start':

start.upto(finish) {|n| ...}

Also there's step, which lets you use an increment other than 1:

start.step(finish, incr) {|n| ...}

It really depends on what you want to do.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-01-30 00:55
(Received via mailing list)
If you want the first 10, just do this:

ary[0..9]

Sent from my iPhone
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-01-30 00:58
(Received via mailing list)
It actually requires a little bit different thinking because once you
know idiomatic ruby, all the 'language' stuff you have to worry about
becomes stuff you just don't have to think about, like building new
arrays pit of existin ones, splitting things up, etc

Sent from my iPhone
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-01-30 01:00
(Received via mailing list)
On Thu, Jan 29, 2009 at 11:24 PM, Dominik Honnef <dominikho@gmx.net>
wrote:
> one solution would be:
> for thing in things[0..9]
> ...
> end
maybe
things.first( 10 ).each do |thing|
   ...
end
is a little bit more idiomatic.
Cheers
Robert
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-01-30 06:46
(Received via mailing list)
Um the beginning is the beginning and the end is the end. It's simple.
You don't have to manage it!

Sent from my iPhone
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-01-30 18:09
(Received via mailing list)
On Fri, Jan 30, 2009 at 6:43 AM, Julian Leviston
<julian@coretech.net.au> wrote:
> Um the beginning is the beginning and the end is the end. It's simple. You
> don't have to manage it!
>
We shall however consider the possibility that the properties of
Enumerable cannot be extended to metaphysics :)
R.
This topic is locked and can not be replied to.