public Method

Kernel.block_given?

block_given?    true or false
iterator?       true or false

Returns true if yield would execute a block in the current context. The iterator? form is mildly deprecated.

def try
  if block_given?
    yield
  else
    "no block"
  end
end
try                  #=> "no block"
try { "hello" }      #=> "hello"
try do "hello" end   #=> "hello"

Source Code

/*
*  call-seq:
*     block_given?   => true or false
*     iterator?      => true or false
*  
*  Returns <code>true</code> if <code>yield</code> would execute a
*  block in the current context. The <code>iterator?</code> form
*  is mildly deprecated.
*     
*     def try
*       if block_given?
*         yield
*       else
*         "no block"
*       end
*     end
*     try                  #=> "no block"
*     try { "hello" }      #=> "hello"
*     try do "hello" end   #=> "hello"
*/


static VALUE
rb_f_block_given_p()
{
   if (ruby_frame->prev && ruby_frame->prev->iter == ITER_CUR && ruby_block)
       return Qtrue;
   return Qfalse;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.