[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Another Q: find behavior

Krekna Mektek

12/29/2006 4:09:00 PM

Hi,

Still learning from the Pickaxe book, and I've got a question about this code:

class Array
def find
for i in 0..size
value = self[i]
puts self[i]
return value if yield(value)
end
return nil
end
end

[1,3,5,7,9].find {|v| v*v > 30 }

irb(main):369:0> [1,3,5,7,9].find {|v| v*v > 30 }
=> 7


how come it prints only 7, and not also 9?
The loop is not stopping as far as I can see, and 9 times 9 is also
bigger then 30.

Krekna

3 Answers

Morton Goldberg

12/29/2006 4:47:00 PM

0

On Dec 29, 2006, at 11:08 AM, Krekna Mektek wrote:

> Hi,
>
> Still learning from the Pickaxe book, and I've got a question about
> this code:
>
> class Array
> def find
> for i in 0..size
> value = self[i]
> puts self[i]
> return value if yield(value)
> end
> return nil
> end
> end
>
> [1,3,5,7,9].find {|v| v*v > 30 }
>
> irb(main):369:0> [1,3,5,7,9].find {|v| v*v > 30 }
> => 7
>
>
> how come it prints only 7, and not also 9?
> The loop is not stopping as far as I can see, and 9 times 9 is also
> bigger then 30.

But the loop does stop when it reaches 7. As soon as 'yield(value)'
evaluates to true, the 'return' is executed and both the method and
the loop terminate.

Regards, Morton

Krekna Mektek

12/29/2006 4:53:00 PM

0

Ah, so the return exits the loop, now I understand.

@Joey:
What I want to achieve? This is just an example from the book ;)

Krekna

2006/12/29, Morton Goldberg <m_goldberg@ameritech.net>:
> On Dec 29, 2006, at 11:08 AM, Krekna Mektek wrote:
>
> > Hi,
> >
> > Still learning from the Pickaxe book, and I've got a question about
> > this code:
> >
> > class Array
> > def find
> > for i in 0..size
> > value = self[i]
> > puts self[i]
> > return value if yield(value)
> > end
> > return nil
> > end
> > end
> >
> > [1,3,5,7,9].find {|v| v*v > 30 }
> >
> > irb(main):369:0> [1,3,5,7,9].find {|v| v*v > 30 }
> > => 7
> >
> >
> > how come it prints only 7, and not also 9?
> > The loop is not stopping as far as I can see, and 9 times 9 is also
> > bigger then 30.
>
> But the loop does stop when it reaches 7. As soon as 'yield(value)'
> evaluates to true, the 'return' is executed and both the method and
> the loop terminate.
>
> Regards, Morton
>
>

Robert Klemme

12/29/2006 4:59:00 PM

0

On 29.12.2006 17:08, Krekna Mektek wrote:
> Hi,
>
> Still learning from the Pickaxe book, and I've got a question about this
> code:
>
> class Array
> def find
> for i in 0..size

That line should read

for i in 0...size

Otherwise you will always throw nil at your test if it failed for all
elements in the array.

> value = self[i]
> puts self[i]
> return value if yield(value)
> end
> return nil
> end
> end
>
> [1,3,5,7,9].find {|v| v*v > 30 }
>
> irb(main):369:0> [1,3,5,7,9].find {|v| v*v > 30 }
> => 7
>
>
> how come it prints only 7, and not also 9?

Because the "return" will immediately make the method exit.

> The loop is not stopping as far as I can see, and 9 times 9 is also
> bigger then 30.

The loop will stop once the first match is found precisely because of
the "return". Either you tested a different piece of code than you
posted or you overlooked something.

Kind regards

robert