Skip to content

Instantly share code, notes, and snippets.

@prodis
Created Jul 18, 2011
Embed
What would you like to do?
Ruby Fundamental - Blocos em Ruby (parte I)
greet { puts "Hello" }
verbose_greet("PuneRuby") { puts "Hello" }
def call_block
puts "Start of method"
yield
yield
puts "End of method"
end
call_block { puts 'In the block' }
Start of method
In the block
In the block
End of method
no block given (LocalJumpError)
def call_block
yield("hello", 99)
end
call_block { |str, num| puts str + " " + num.to_s}
def try
if block_given?
yield
else
puts "no block"
end
end
try # => "no block"
try { puts "hello" } # => "hello"
try do
puts "hello"
end
# => "hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment