A lambda is a special kind of anonymous function or block that can be stored in a variable and called later. It behaves like a method and can take arguments and return values.
Click to reveal answer
beginner
How do you create a lambda in Ruby?
You can create a lambda using either lambda { |args| ... } or ->(args) { ... } syntax.
Click to reveal answer
intermediate
What happens if you use return inside a lambda?
Using return inside a lambda exits only from the lambda itself, not from the surrounding method or block.
Click to reveal answer
intermediate
How does a lambda differ from a Proc in Ruby?
A lambda checks the number of arguments passed and treats return differently (returns from lambda only). A Proc does not check arguments strictly and return exits from the enclosing method.
Click to reveal answer
beginner
How do you call a lambda stored in a variable?
You call a lambda by using the .call method on the variable, like my_lambda.call(args).
Click to reveal answer
Which syntax creates a lambda in Ruby?
Aproc { |x| x * 2 }
B->(x) { x * 2 }
Cdef lambda(x); x * 2; end
Dfunction(x) { return x * 2 }
✗ Incorrect
The arrow syntax ->(x) { ... } creates a lambda in Ruby.
What happens if you pass the wrong number of arguments to a lambda?
AIt raises an ArgumentError.
BIt ignores extra arguments.
CIt sets missing arguments to nil.
DIt runs without error.
✗ Incorrect
Lambdas check argument count strictly and raise ArgumentError if it doesn't match.
What does return inside a lambda do?
ARaises an error.
BReturns from the enclosing method.
CReturns from the lambda only.
DExits the program.
✗ Incorrect
Return inside a lambda exits only the lambda, not the surrounding method.
How do you execute a lambda stored in variable f?
Af.execute()
Bf.run()
Ccall f()
Df.call()
✗ Incorrect
You call a lambda using the .call method.
Which is true about lambdas compared to Procs?
ALambdas check arguments and Procs do not.
BProcs check arguments and Lambdas do not.
CBoth behave exactly the same.
DLambdas cannot be stored in variables.
✗ Incorrect
Lambdas check argument count strictly, Procs do not.
Explain how to create and call a lambda in Ruby, including the syntax options.
Think about the arrow syntax and the lambda keyword.
You got /3 concepts.
Describe the difference in behavior between a lambda and a Proc when using return and argument checking.
Focus on how return and arguments behave differently.
You got /4 concepts.
Practice
(1/5)
1. What is a lambda in Ruby?
easy
A. A type of loop that repeats code multiple times
B. A special variable that holds numbers only
C. A method that automatically runs when a program starts
D. A small block of code stored in a variable that can be called later
Solution
Step 1: Understand what a lambda represents
A lambda is a reusable piece of code stored in a variable, like a mini-function.
Step 2: Compare with other options
Options A, C, and D describe loops, methods, or variables, not lambdas.
Final Answer:
A small block of code stored in a variable that can be called later -> Option D
Quick Check:
Lambda = reusable code block [OK]
Hint: Lambdas are like mini-functions stored in variables [OK]
Common Mistakes:
Confusing lambdas with loops
Thinking lambdas run automatically
Believing lambdas hold only numbers
2. Which of the following is the correct way to create a lambda in Ruby?
easy
A. ->(x) { x * 2 }
B. def lambda(x) x * 2 end
C. lambda(x) => x * 2
D. lambda(x) { x * 2 }
Solution
Step 1: Identify valid lambda syntax
In Ruby, lambdas can be created using the arrow syntax ->(params) { code } or lambda { |params| code }.
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.
Final Answer:
->(x) { x * 2 } -> Option A
Quick Check:
Arrow syntax ->(x) { ... } creates lambda [OK]
Hint: Use ->(params) { code } or lambda { |params| code } [OK]
Common Mistakes:
Using def instead of lambda
Incorrect arrow syntax
Confusing method and lambda syntax
3. What will be the output of this Ruby code?
my_lambda = ->(x) { x + 3 }
puts my_lambda.call(4)
medium
A. 3
B. 4
C. 7
D. Error
Solution
Step 1: Understand the lambda behavior
The lambda adds 3 to the input number x.
Step 2: Calculate the output
Calling my_lambda.call(4) computes 4 + 3 = 7.
Final Answer:
7 -> Option C
Quick Check:
4 + 3 = 7 [OK]
Hint: Call lambda with .call and input to get output [OK]
Common Mistakes:
Forgetting to use .call to invoke lambda
Confusing input and output values
Expecting syntax error
4. What is wrong with this Ruby code?
my_lambda = lambda { |x, y| x + y }
puts my_lambda.call(5)
medium
A. It will raise an ArgumentError
B. It will print nil
C. It will print 5
D. It will raise a SyntaxError
Solution
Step 1: Check lambda argument requirements
Lambdas check arguments strictly. This lambda expects 2 arguments (x and y).
Step 2: Analyze the call with one argument
Calling my_lambda.call(5) provides only 1 argument, causing ArgumentError.