📖 Scenario: You are working on a small Ruby program that uses lambdas to perform simple math operations. Lambdas are like little machines that take input and give output. You will create a lambda, set up a value to use with it, run the lambda, and then show the result.
🎯 Goal: Build a Ruby program that creates a lambda to double a number, applies it to a given number, and prints the result.
📋 What You'll Learn
Create a lambda called double that takes one argument and returns it multiplied by 2
Create a variable called number and set it to 7
Call the double lambda with number as input and store the result in doubled_number
Print the value of doubled_number
💡 Why This Matters
🌍 Real World
Lambdas are useful for small tasks you want to reuse, like math operations or filtering lists.
💼 Career
Understanding lambdas helps you write cleaner, more flexible Ruby code, which is valuable in many programming jobs.
Progress0 / 4 steps
1
Create a lambda called double
Create a lambda called double that takes one argument named x and returns x * 2.
Ruby
Hint
Use ->(x) { } to create a lambda in Ruby.
2
Create a variable number with value 7
Create a variable called number and set it to 7.
Ruby
Hint
Just write number = 7 to store the number.
3
Call the lambda and store the result
Call the lambda double with number as input and store the result in a variable called doubled_number.
Ruby
Hint
Use double.call(number) to run the lambda.
4
Print the doubled number
Print the value of doubled_number using puts.
Ruby
Hint
Use puts doubled_number to show the result.
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.