Created
July 24, 2011 21:01
-
-
Save prodis/1103096 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Procs e lambdas em Ruby
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 { |x, y| x * y } |
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 my_method(&block) | |
block | |
end | |
my_proc = my_method { |x, y| x * y } | |
my_proc.class # => Proc |
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 { |x, y| x * y } |
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 = ->(x, y) { x * y } |
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 = ->(x, y=2) { x * y } |
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 = -> x, y=2 { x * y } |
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
addition = Proc.new { |x, y| x + y } | |
addition.call(3, 2) # => 5 | |
subtraction = lambda { |x, y| x - y } | |
subtraction.call(3, 2) # => 1 | |
multiplication = -> x, y { x * y } | |
multiplication.call(3, 2) # => 6 |
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
addition = Proc.new { |x, y| x + y } | |
result = addition[10,20] | |
puts result # => 30 |
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
addition = Proc.new { |x, y| x + y } | |
result = addition.(10, 20) | |
puts result # => 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment