Skip to content

Instantly share code, notes, and snippets.

@prodis
Created July 18, 2011 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodis/1088482 to your computer and use it in GitHub Desktop.
Save prodis/1088482 to your computer and use it in GitHub Desktop.
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