On Feb 18, 2006, at 12:32 PM, bbqbaker wrote:

> 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[i]
>       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
>

Basically yes:

however if I was writing in C++ I'd probably say:

bool code_blk(int v)
{
    return v * v > 30;
}

the extra branch is really unecessary


> -- 
> Posted via http://www.ruby-forum.com/.
>