Lambdas let you create small pieces of reusable code that you can run later. They help keep your code neat and organized.
Lambda creation and behavior in Ruby
my_lambda = ->(parameters) { code }
# or
my_lambda = lambda { |parameters| code }The arrow syntax -> is a short way to create a lambda.
Lambdas check the number of arguments strictly and return control to the calling method when they finish.
greet = ->(name) { puts "Hello, #{name}!" }
greet.call("Alice")square = lambda { |x| x * x } puts square.call(4)
adder = ->(a, b) { a + b }
result = adder.call(3, 5)
puts resultThis program creates a lambda that greets a person by name. It shows how calling it without arguments causes an error because lambdas check arguments strictly. It also shows how a return inside a lambda only exits the lambda, not the whole method.
say_hello = ->(name) { "Hello, #{name}!" }
puts say_hello.call("Bob")
# Showing argument check
begin
say_hello.call
rescue ArgumentError => e
puts "Error: #{e.message}"
end
# Showing return behavior
def test_lambda
l = -> { return "Returned from lambda" }
result = l.call
return "Returned from method after lambda: #{result}"
end
puts test_lambda()Lambdas are like methods inside variables.
They check the number of arguments strictly, unlike regular blocks.
Return inside a lambda exits only the lambda, not the surrounding method.
Lambdas are small, reusable pieces of code you can store in variables.
They check arguments strictly and have special return behavior.
You create them using -> or lambda keywords.
Practice
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 DQuick Check:
Lambda = reusable code block [OK]
- Confusing lambdas with loops
- Thinking lambdas run automatically
- Believing lambdas hold only numbers
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 AQuick Check:
Arrow syntax ->(x) { ... } creates lambda [OK]
- Using def instead of lambda
- Incorrect arrow syntax
- Confusing method and lambda syntax
my_lambda = ->(x) { x + 3 }
puts my_lambda.call(4)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 CQuick Check:
4 + 3 = 7 [OK]
- Forgetting to use .call to invoke lambda
- Confusing input and output values
- Expecting syntax error
my_lambda = lambda { |x, y| x + y }
puts my_lambda.call(5)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.Final Answer:
It will raise an ArgumentError -> Option AQuick Check:
Lambda argument count mismatch = ArgumentError [OK]
- Assuming missing arguments default to nil
- Expecting no error on fewer arguments
- Confusing lambdas with procs (which are lenient)
def test_lambda
l = -> { return "Inside lambda" }
result = l.call
return "Outside lambda"
end
puts test_lambdaSolution
Step 1: Understand lambda return behavior
Return inside a lambda exits only the lambda, not the enclosing method.Step 2: Trace method execution
The lambda returns "Inside lambda" to variable result, but method continues and returns "Outside lambda".Final Answer:
Outside lambda -> Option BQuick Check:
Lambda return exits lambda only, method returns after [OK]
- Thinking return inside lambda exits method
- Expecting "Inside lambda" as output
- Confusing lambda with proc return behavior
Ready to prove it?
Pass the test and this pattern moves unknown
