# Another Q: find behavior

**URL:** <https://www.ruby-forum.com/t/another-q-find-behavior/80648>\
**Category:** Ruby\
**Created:** [December 29, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648 "2006-12-29T17:10:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648/1 "2006-12-29T17:10:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648/2 "2006-12-29T17:24:43Z")

</div>

Because of the “return value if yield(value)”, which returns from the  
method  
causing the loop to end.

I’m not sure what your really trying to achieve, there already is a  
Array#find

j`ey

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648/3 "2006-12-29T17:48:50Z")

</div>

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

> ```
> return value if yield(value)
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648/4 "2006-12-29T17:54:52Z")

</div>

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 G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/another-q-find-behavior/80648/5 "2006-12-29T18:00:54Z")

</div>

On 29.12.2006 17:08, Krekna M. 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.

> 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
```
