Blocks, Procs, and Lambdas?

  • Block: Anonymous piece of code passed to methods.
3.times { puts "Hello" }
  • Proc: Saved block that can be reused.
say = Proc.new { puts "Hi" }
say.call
  • Lambda: Like a Proc but stricter about arguments.
l = ->(x) { x*x }
puts l.call(5) # 25

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *