Bird
0
0

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

easy📝 Syntax Q12 of 15
Ruby - Blocks, Procs, and Lambdas
Which of the following is the correct way to create a lambda in Ruby?
A->(x) { x * 2 }
Bdef lambda(x) x * 2 end
Clambda(x) => x * 2
Dlambda(x) { x * 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Identify valid lambda syntax

    In Ruby, lambdas can be created using the arrow syntax ->(params) { code } or lambda { |params| code }.
  2. Step 2: Check each option

    lambda(x) { x * 2 }: invalid syntax (lambda keyword expects block with |params|, not parenthesized args). def lambda(x) x * 2 end: method definition, not a lambda. lambda(x) => x * 2: invalid syntax. ->(x) { x * 2 }: correct arrow syntax.
  3. Final Answer:

    ->(x) { x * 2 } -> Option A
  4. Quick Check:

    Arrow syntax ->(x) { ... } creates lambda [OK]
Quick Trick: Use ->(params) { code } or lambda { |params| code } [OK]
Common Mistakes:
  • Using def instead of lambda
  • Incorrect arrow syntax
  • Confusing method and lambda syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes