Skip to content

Instantly share code, notes, and snippets.

@prodis
Created July 24, 2011 21:01
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/1103096 to your computer and use it in GitHub Desktop.
Save prodis/1103096 to your computer and use it in GitHub Desktop.
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