Created
August 28, 2011 21:02
-
-
Save prodis/1177225 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Diferenças entre procs e lambdas
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 some_method | |
puts "Início do método" | |
my_proc = Proc.new do |message| | |
puts "Dentro do proc: #{message}" | |
return | |
end | |
my_proc.call "Ruby Fundamental" | |
puts "Fim do método" # Essa linha não é executada | |
end | |
some_method | |
# Início do método | |
# Dentro do proc: Ruby Fundamental |
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
my_proc = Proc.new do |message| | |
puts "Dentro do proc: #{message}" | |
return | |
end | |
def some_method(executable) | |
puts "Início do método" | |
executable.call "Ruby Fundamental" # Aqui será lançado um LocalJumpError | |
puts "Fim do método" | |
end | |
some_method(my_proc) | |
# Início do método | |
# Dentro do proc: Ruby Fundamental | |
# LocalJumpError: unexpected return |
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 some_method | |
puts "Início do método" | |
my_lambda = lambda do |message| | |
puts "Dentro do lambda: #{message}" | |
return | |
end | |
my_lambda.call "Ruby Fundamental" | |
puts "Fim do método" | |
end | |
some_method | |
# Início do método | |
# Dentro do lambda: Ruby Fundamental | |
# Fim do método |
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
my_lambda = lambda do |message| | |
puts "Dentro do lambda: #{message}" | |
return | |
end | |
def some_method(executable) | |
puts "Início do método" | |
executable.call "Ruby Fundamental" | |
puts "Fim do método" | |
end | |
some_method(my_lambda) | |
# Início do método | |
# Dentro do lambda: Ruby Fundamental | |
# Fim do método |
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
full_name = lambda { |first_name, last_name| puts "#{first_name} #{last_name}" } | |
full_name.call "Fernando", "de Amorim" | |
# => Fernando de Amorim | |
full_name.call "Fernando" | |
# => ArgumentError: wrong number of arguments (1 for 2) | |
full_name.call "Fernando", "Hamasaki", "de Amorim" | |
# => ArgumentError: wrong number of arguments (3 for 2) | |
full_name.call ["Fernando", "de Amorim"] | |
# => ArgumentError: wrong number of arguments (1 for 2) | |
full_name.call ["Fernando", "Hamasaki", "de Amorim"] | |
# => ArgumentError: wrong number of arguments (1 for 2) | |
full_name.call *["Fernando", "de Amorim"] | |
# => Fernando de Amorim | |
full_name.call *["Fernando", "Hamasaki", "de Amorim"] | |
# => ArgumentError: wrong number of arguments (3 for 2) |
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
full_name = Proc.new { |first_name, last_name| puts "#{first_name} #{last_name}" } | |
full_name.call "Fernando", "de Amorim" | |
# => Fernando de Amorim | |
full_name.call "Fernando" | |
# => Fernando | |
full_name.call "Fernando", "Hamasaki", "de Amorim" | |
# => Fernando Hamasaki | |
full_name.call ["Fernando", "de Amorim"] | |
# => Fernando de Amorim | |
full_name.call ["Fernando", "Hamasaki", "de Amorim"] | |
# => Fernando Hamasaki | |
full_name.call *["Fernando", "de Amorim"] | |
# => Fernando de Amorim | |
full_name.call *["Fernando", "Hamasaki", "de Amorim"] | |
# => Fernando Hamasaki |
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
full_name = lambda { |first_name, last_name| puts "#{first_name} #{last_name}" } | |
full_name.arity # => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment