Bird
0
0

Which of the following is a valid way to create a lambda in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Blocks, Procs, and Lambdas
Which of the following is a valid way to create a lambda in Ruby?
Amy_lambda = ->(x) { x * 2 }
Bmy_lambda = proc { |x| x * 2 }
Cmy_lambda = function(x) { x * 2 }
Dmy_lambda = lambda { |x| x * 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Identify valid lambda syntax

    In Ruby, lambdas can be created using lambda { |args| ... } or the arrow syntax -> my_lambda = ->(x) { x * 2 }
  2. Step 2: Check each option

    my_lambda = lambda { |x| x * 2 } uses lambda with block and arguments correctly. my_lambda = proc { |x| x * 2 } creates a proc, not a lambda. my_lambda = function(x) { x * 2 } uses invalid Ruby syntax. my_lambda = ->(x) { x * 2 } uses correct arrow lambda syntax with parentheses.
  3. Final Answer:

    my_lambda = lambda { |x| x * 2 } and my_lambda = ->(x) { x * 2 } are valid lambda definitions.
  4. Quick Check:

    Valid lambda syntax = A and D [OK]
Quick Trick: Use lambda { |args| ... } or ->(args) { ... } [OK]
Common Mistakes:
  • Confusing proc with lambda syntax
  • Omitting parentheses in arrow syntax
  • Using non-Ruby function syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes