A
Adrian Arlechin
I have this example:
class Array
def find
for i in 0...size
value = self
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
class Array
def find
for i in 0...size
value = self
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