Skip to content
Tags

,

block_given?

April 7, 2011

You can choose to send a block or not to a method that you define. block_given? could be used to generate a conditional yield:

def test_block_given?

puts "Got in the method"

if block_given?

yield

else

puts "no block found!"

end

end

=> nil

ruby-1.9.2-p0 :059 > test_block_given?

Got in the method

no block found!

=> nil

ruby-1.9.2-p0 :060 > test_block_given?{ puts "this will be printed"}

Got in the method

this will be printed

=> nil
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: