B
bbqbaker
I hi,
I am new to ruby and bought my first book. i am having sort of difficult
time understanding blocks/yeild statements. i am coming from C++.
class Array
def find
for i in 0..size
value = self
return value if yield(value)
end
return nil
end
end
[1,3,5,7,9].find {|v| v*v > 30}
-> 7
in the last line of code, it is passing to function find each number one
by one thats on the left? when it hits the "return value if
yield(value)" it will pass parameter value to the block and v will now
be equal to value, and then perform the code v*v > 30. if that is true,
it will get out of fuction find.
so what is in between the |" "| of the block is like in c++:
bool code_blk(int v)
{
if (v * v > 30)
return TRUE;
else
return FALSE;
}
is this kind of right?? thanks
I am new to ruby and bought my first book. i am having sort of difficult
time understanding blocks/yeild statements. i am coming from C++.
class Array
def find
for i in 0..size
value = self
return value if yield(value)
end
return nil
end
end
[1,3,5,7,9].find {|v| v*v > 30}
-> 7
in the last line of code, it is passing to function find each number one
by one thats on the left? when it hits the "return value if
yield(value)" it will pass parameter value to the block and v will now
be equal to value, and then perform the code v*v > 30. if that is true,
it will get out of fuction find.
so what is in between the |" "| of the block is like in c++:
bool code_blk(int v)
{
if (v * v > 30)
return TRUE;
else
return FALSE;
}
is this kind of right?? thanks