14/21 Lambdas vs. Procs [returned nil]


#1

I understand what's going on in this code, but why is nil returned but not "Iron man will win" if that is what the last line does?

def batman_ironman_proc
  victor = Proc.new { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  victor = lambda { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_lambda

Returned

Batman will win!
Iron Man will win!
nil


#2

The "Batman will win!" output comes from the proc.

The "Iron Man will win!" output comes from the lambda.

If you want to check that, just modify the strings, you'll see.

nil is returned by puts, and Ruby shows what the last instruction returns after everything else. It's not part of the output.