Bird
0
0

Which of the following is the correct syntax to create a lambda in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Blocks, Procs, and Lambdas
Which of the following is the correct syntax to create a lambda in Ruby?
Alambda = Proc.new { |x| x * 2 }
Blambda = proc { |x| x * 2 }
Clambda = ->(x) { x * 2 }
Dlambda = lambda.new { |x| x * 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Identify lambda creation syntax

    In Ruby, lambdas can be created using the literal syntax ->(args) { block } or lambda { block }. The arrow syntax is common and concise.
  2. Step 2: Check each option

    lambda = Proc.new { |x| x * 2 } creates a Proc, not a lambda. lambda = ->(x) { x * 2 } uses the correct lambda literal syntax. lambda = proc { |x| x * 2 } creates a Proc using proc. lambda = lambda.new { |x| x * 2 } is invalid syntax.
  3. Final Answer:

    lambda = ->(x) { x * 2 } -> Option C
  4. Quick Check:

    Lambda syntax = ->(args) { block } [OK]
Quick Trick: Use ->(args) { } for lambda, Proc.new or proc for Proc [OK]
Common Mistakes:
  • Using Proc.new when lambda is needed
  • Trying lambda.new which doesn't exist
  • Confusing proc and lambda syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes