Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Fundamental - Procs e lambdas em Ruby
my_proc = Proc.new { |x, y| x * y }
def my_method(&block)
block
end
my_proc = my_method { |x, y| x * y }
my_proc.class # => Proc
my_lambda = lambda { |x, y| x * y }
my_lambda = ->(x, y) { x * y }
my_lambda = ->(x, y=2) { x * y }
my_lambda = -> x, y=2 { x * y }
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
addition = Proc.new { |x, y| x + y }
result = addition[10,20]
puts result # => 30
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