×

ruby · level 3 · unit 1 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a proc with strict rules.

Write a lambda with an arrow: ->(weight, zone) { weight * zone }, or with lambda do |weight, zone| for longer ones. Like a proc, you keep it in a variable and run it with .call. Unlike a proc, a lambda is strict about its , the number of arguments it expects.

Give a lambda the wrong number and it raises . A proc shrugs instead: a missing argument becomes nil and extras are dropped. That silence hides bugs, so professionals reach for lambdas when they store a rule or a calculation in a variable.

A lambda also treats return like a method does: it leaves the lambda only, and the code around it carries on.

shipping = ->(weight, zone) { weight * zone }
loose = proc { |weight, zone| [weight, zone] }
puts shipping.call(2, 3)
puts shipping.arity
p loose.call(2)

A proc's return tries to leave the whole method around it, which surprises people, so lambdas are the safer choice.