Ruby - Blocks, Procs, and Lambdas
You want to create a Proc that returns the square of a number and then call it for numbers 1 to 3, collecting results in an array. Which code correctly does this?
proc { |n| n * n } or equivalent n ** 2.(1..3).map { |x| square.call(x) } to collect [1, 4, 9]; each returns original range, missing .call or arg causes error.puts results.inspect prints [1, 4, 9].15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions