From: matt@... (matt neuburg) Date: 2009-06-06T02:05:09+09:00 Subject: Re: Test array and assign result to variable Adrian Arlechin wrote: > 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 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. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Leopard - http://www.takecontrolbooks.com/leopard-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com