# Test array and assign result to variable

**URL:** <https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592>\
**Category:** Ruby\
**Created:** [June 5, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592 "2009-06-05T18:22:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [June 5, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592/1 "2009-06-05T18:22:09Z")

</div>

I have this example:  
class Array  
def find  
for i in 0…size  
value = self[i]  
return value if yield(value)  
end  
return nil  
end  
end  
x = [1, 3, 5, 7, 9]  
x.find {|v| v_v \> 30 }  
It says that return 7 - the first value wich match the condition  
I run it in NetBeans but don’t see the output  
I tried this:  
x.find {|v|  
if v_v \> 30  
puts v  
end } but now prints the both values that match the condition: 7, 9  
So the problem is how can I see only the first match, and most important  
how can I extract this value to a variable to use it somewhere else.  
Because in general I want to test an array for a condition, and then use  
the results  
Thanks

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [June 5, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592/2 "2009-06-05T19:05:37Z")

</div>

Adrian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x = [1, 3, 5, 7, 9]  
> x.find {|v| v\*v \> 30 }  
> It says that return 7 - the first value wich match the condition  
> I run it in NetBeans but don’t see the output

Because you didn’t ask for any output. It would suffice to change the  
last line from

x.find {|v| v\*v \> 30 }

to

puts x.find {|v| v\*v \> 30 }

> I tried this:  
> x.find {|v|  
> if v\*v \> 30  
> puts v  
> end } but now prints the both values that match the condition: 7, 9  
> So the problem is how can I see only the first match, and most important  
> how can I extract this value to a variable to use it somewhere else.  
> Because in general I want to test an array for a condition, and then use  
> the results

It’s good that you’re trying to “roll your own” on this, but still,  
Array#find already exists and does exactly what you’re looking for.

m.

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [June 6, 2009, 12:40am UTC](https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592/3 "2009-06-06T00:40:01Z")

</div>

Thank you,  
it works. I have complicated myself!  
But how I retain this result in a variable to work with it.  
Because I think that must be situations in wich you put results from  
some calculations in an array, and then due tu a condition, you want to  
take that specific value to work further with it  
Adrian

---

<div class="post-metadata">

**Author:** ![adrianunderwater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@adrianunderwater](https://www.ruby-forum.com/u/adrianunderwater)\
**Post date:** [June 6, 2009, 1:59am UTC](https://www.ruby-forum.com/t/test-array-and-assign-result-to-variable/169592/4 "2009-06-06T01:59:54Z")

</div>

I respond to myself.  
Of corse the answer is  
x = [1, 3, 5, 7, 9]  
y = x.find {|v| v_v \> 30 } #and now I have the variable  
puts y =\> 7  
Sorry, I am a very beginer and I was iduced in error because in the  
book the ex was [1, 3, 5, 7, 9].find {|v| v_v \> 30 } and it didn’t came  
to me to write puts before such a expresion, and I worked on v.  
Thanks again  
Adrian
