Bird
0
0

Which of the following Ruby lambda definitions will cause a syntax error?

easy📝 Syntax Q3 of 15
Ruby - Blocks, Procs, and Lambdas
Which of the following Ruby lambda definitions will cause a syntax error?
Amy_lambda = ->(x) { x + 1 }
Bmy_lambda = lambda do |x| x + 1 end
Cmy_lambda = lambda { |x| x + 1 }
Dmy_lambda = -> x { x + 1 }
Step-by-Step Solution
Solution:
  1. Step 1: Review arrow lambda syntax rules

    The arrow lambda syntax requires parentheses around parameters: ->(x) { ... }.
  2. Step 2: Identify invalid syntax

    my_lambda = -> x { x + 1 } misses parentheses around x, causing a syntax error. Other options are valid lambda definitions.
  3. Final Answer:

    my_lambda = -> x { x + 1 } -> Option D
  4. Quick Check:

    Arrow lambda needs parentheses = C [OK]
Quick Trick: Arrow lambdas need parentheses around parameters [OK]
Common Mistakes:
  • Omitting parentheses in arrow lambdas
  • Confusing do-end and braces syntax
  • Using invalid Ruby function syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes