Created
July 18, 2011 03:18
-
-
Save prodis/1088482 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Blocos em Ruby (parte I)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
greet { puts "Hello" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
verbose_greet("PuneRuby") { puts "Hello" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_block | |
puts "Start of method" | |
yield | |
yield | |
puts "End of method" | |
end | |
call_block { puts 'In the block' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Start of method | |
In the block | |
In the block | |
End of method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
no block given (LocalJumpError) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_block | |
yield("hello", 99) | |
end | |
call_block { |str, num| puts str + " " + num.to_s} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello 99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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