[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Normal For Loop

Cory Cory

3/3/2008 2:10:00 PM

I'm new to Ruby. Is there such thing as a C++ style (and most others)
for loop in Ruby? What I mean by that is:

for( init, while-condition, increment ) { function-here }

I know I can do that with a while loop, but I enjoy the compactness of
the above statement. There are many times where Ruby's for-each loops
just don't do the trick.

Also, is there anything like i++ or do I always have to do i=i+1 ?
--
Posted via http://www.ruby-....

12 Answers

Peter Hickman

3/3/2008 2:17:00 PM

0

Cory Cory wrote:
> I'm new to Ruby. Is there such thing as a C++ style (and most others)
> for loop in Ruby? What I mean by that is:
>
> for( init, while-condition, increment ) { function-here }
>
> I know I can do that with a while loop, but I enjoy the compactness of
> the above statement. There are many times where Ruby's for-each loops
> just don't do the trick.
>

We would be very interested to see a loop in C that cannot be rewritten
in Ruby. Care to give us an example?

> Also, is there anything like i++ or do I always have to do i=i+1 ?
>
You can use i += 1.

Jano Svitok

3/3/2008 2:20:00 PM

0

On Mon, Mar 3, 2008 at 3:10 PM, Cory Cory <cg821105@ohio.edu> wrote:
> I'm new to Ruby. Is there such thing as a C++ style (and most others)
> for loop in Ruby? What I mean by that is:
>
> for( init, while-condition, increment ) { function-here }
>
> I know I can do that with a while loop, but I enjoy the compactness of
> the above statement. There are many times where Ruby's for-each loops
> just don't do the trick.

There is 5.times {|i| } if the count is known at cycle start.
Then you have array.each {}, and the whole bunch from Enumerable
(each_with_index, select, find, map, inject, ...)

elof

3/3/2008 2:24:00 PM

0


On Mon, 3 Mar 2008 23:10:09 +0900, Cory Cory <cg821105@ohio.edu> wrote:
> I'm new to Ruby. Is there such thing as a C++ style (and most others)
> for loop in Ruby? What I mean by that is:
>
> for( init, while-condition, increment ) { function-here }
>
> I know I can do that with a while loop, but I enjoy the compactness of
> the above statement. There are many times where Ruby's for-each loops
> just don't do the trick.

["foo", "bar", "baz"].each_with_index {|elem, i|
puts "#{elem} #{i}"
}


Kristian




jwmerrill@gmail.com

3/3/2008 2:28:00 PM

0

On Mar 3, 9:10 am, Cory Cory <cg821...@ohio.edu> wrote:
> I'm new to Ruby. Is there such thing as a C++ style (and most others)
> for loop in Ruby? What I mean by that is:
>
> for( init, while-condition, increment ) { function-here }

for n in 1..10
puts n
end

It seems more common in ruby to do things like

1.upto(10) {|n| puts n}

or

(1..10).each {|n| puts n}

If you mean that you want your condition and your increment to depend
on things that you won't know about until after the loop has been
running for a while, then yeah, you might have to use a while loop.
This doesn't seem to come up much, though. Perhaps you could post a
specific example.

> I know I can do that with a while loop, but I enjoy the compactness of
> the above statement. There are many times where Ruby's for-each loops
> just don't do the trick.

With a little creativity, not so many, in my experience.

> Also, is there anything like i++ or do I always have to do i=i+1 ?

i=i+1 is the way to do it. There is some rationale for this, but I
don't remember what it is.

JM

Tim Hunter

3/3/2008 2:31:00 PM

0

Jano Svitok wrote:
> On Mon, Mar 3, 2008 at 3:10 PM, Cory Cory <cg821105@ohio.edu> wrote:
>> I'm new to Ruby. Is there such thing as a C++ style (and most others)
>> for loop in Ruby? What I mean by that is:
>>
>> for( init, while-condition, increment ) { function-here }
>>
>> I know I can do that with a while loop, but I enjoy the compactness of
>> the above statement. There are many times where Ruby's for-each loops
>> just don't do the trick.
>
> There is 5.times {|i| } if the count is known at cycle start.
> Then you have array.each {}, and the whole bunch from Enumerable
> (each_with_index, select, find, map, inject, ...)

Not to mention Numeric#step:

1.step(10, 2) { |i| print i, " " }

And Integer#upto:

5.upto(10) { |i| print i, " " }
--
Posted via http://www.ruby-....

Cory Cory

3/3/2008 2:38:00 PM

0

I understand that all of these for-each style loops are available, but I
don't want to always go through the entire loop, sometimes I want to
stop at some earlier condition.

I tried to do this the other night, but right now I can't find a good
example so here it goes:

a = some_array
minValue = 999999
for(i=0; i<a.length && minValue!=0; i+=1) {
minValue = (5000 - a[i]).abs
}


This isn't the best example. However, there are many times like the
loop above where I want to go through the whole thing, but if I find
exactly what I am looking for, I want to bail out early instead of
wasting that processing time.

Also, I sometimes may want to not actually iterate straight through, but
browse through the array in some more complex order.
--
Posted via http://www.ruby-....

Jano Svitok

3/3/2008 2:44:00 PM

0

On Mon, Mar 3, 2008 at 3:29 PM, jwmerrill@gmail.com <jwmerrill@gmail.com> wrote:
> i=i+1 is the way to do it. There is some rationale for this, but I
> don't remember what it is.

IIRC, the rationale is that variables in ruby are just references to
objects, so you cannot call methods
on variables themselves. You call methods on objects they reference.
That means a call (except assignment)
cannot change the object a variable points to.

i.e. i = 3 that means, variable i points to/references object 3 of class Fixnum.
if there was a call ++, (i++) that would mean the same as 3++. 3++
call can be written so that it returns 4
(actually it's called 3.succ), but there's no way to assign 4 back to
i. It would still point to 3. And because Fixnums are
singletons (there's only one "3" object) they are read only, so
there's no 3.succ!

jwmerrill@gmail.com

3/3/2008 2:55:00 PM

0

On Mar 3, 9:37 am, Cory Cory <cg821...@ohio.edu> wrote:
> I tried to do this the other night, but right now I can't find a good
> example so here it goes:
>
> a = some_array
> minValue = 999999
> for(i=0; i<a.length && minValue!=0; i+=1) {
> minValue = (5000 - a[i]).abs
>
> }

What does this example do exactly? It seems to run until it finds a
value in a that is equal to 5000, and then stop without reporting
anything.

a.detect {|n| 5000 == n}

or

a.any? {|n| 5000 == n}

See, this is actually more concise and readable, I think.

> This isn't the best example. However, there are many times like the
> loop above where I want to go through the whole thing, but if I find
> exactly what I am looking for, I want to bail out early instead of
> wasting that processing time.
>
> Also, I sometimes may want to not actually iterate straight through, but
> browse through the array in some more complex order.

The point is not that no examples exist, but that they realistically
don't come up very much. Get familiar with the methods in Enumerable,
Array, and Numeric. Each time you want to write a for loop, try and
use one of those instead, and if you get stuck, ask for advice.
You're solution will almost always be more readable, and maybe 1 time
in 100 you'll need to use a while loop.

JM

Stefano Crocco

3/3/2008 2:58:00 PM

0

Alle Monday 03 March 2008, jwmerrill@gmail.com ha scritto:
> On Mar 3, 9:37 am, Cory Cory <cg821...@ohio.edu> wrote:
> > I tried to do this the other night, but right now I can't find a good
> > example so here it goes:
> >
> > a = some_array
> > minValue = 999999
> > for(i=0; i<a.length && minValue!=0; i+=1) {
> > minValue = (5000 - a[i]).abs
> >
> > }
>
> What does this example do exactly? It seems to run until it finds a
> value in a that is equal to 5000, and then stop without reporting
> anything.
>
> a.detect {|n| 5000 == n}
>
> or
>
> a.any? {|n| 5000 == n}
>
> See, this is actually more concise and readable, I think.
>
> > This isn't the best example. However, there are many times like the
> > loop above where I want to go through the whole thing, but if I find
> > exactly what I am looking for, I want to bail out early instead of
> > wasting that processing time.
> >
> > Also, I sometimes may want to not actually iterate straight through, but
> > browse through the array in some more complex order.
>
> The point is not that no examples exist, but that they realistically
> don't come up very much. Get familiar with the methods in Enumerable,
> Array, and Numeric. Each time you want to write a for loop, try and
> use one of those instead, and if you get stuck, ask for advice.
> You're solution will almost always be more readable, and maybe 1 time
> in 100 you'll need to use a while loop.
>
> JM

If you want to exit the loop before all iterations are done, you can use
break.

Stefano


mockturtle

3/3/2008 3:03:00 PM

0



Cory Cory ha scritto:

> I understand that all of these for-each style loops are available, but I
> don't want to always go through the entire loop, sometimes I want to
> stop at some earlier condition.
>
> I tried to do this the other night, but right now I can't find a good
> example so here it goes:
>
> a = some_array
> minValue = 999999
> for(i=0; i<a.length && minValue!=0; i+=1) {
> minValue = (5000 - a[i]).abs
> }
>
>
> This isn't the best example. However, there are many times like the
> loop above where I want to go through the whole thing, but if I find
> exactly what I am looking for, I want to bail out early instead of
> wasting that processing time.

In this case you can use "break" construct (which I, personally,
love). For example,

minValue = 999999
a.each do |x|
minValue = (5000-x).abs
break if minValue==0
end

(I did not actually try it, but it should work)
>
> Also, I sometimes may want to not actually iterate straight through, but
> browse through the array in some more complex order.

In this case I'm afraid that you must resort to a while... (BTW, I
always considered
C-style "for" as a "while" in disguise... ;-)
> --
> Posted via http://www.ruby-....