📖 Scenario: Imagine you are creating a simple greeting card generator. Each card has a personalized message that includes the recipient's name and a special number. You want to create a function that remembers the special number and uses it whenever you greet someone.
🎯 Goal: Build a Ruby program that demonstrates how closures capture variable bindings by creating a greeting function that remembers a special number and uses it to greet different people.
📋 What You'll Learn
Create a variable to hold a special number
Define a closure (lambda) that uses the special number and a name parameter
Change the special number after defining the closure
Call the closure with different names to see how variable binding works
💡 Why This Matters
🌍 Real World
Closures are used in Ruby for callbacks, event handlers, and to create functions that remember state without using global variables.
💼 Career
Understanding closures helps in writing clean, modular Ruby code and is important for roles involving Ruby on Rails or scripting.
Progress0 / 4 steps
1
Create a variable called special_number and set it to 7
Create a variable called special_number and set it to the integer 7.
Ruby
Hint
Use = to assign the value 7 to special_number.
2
Define a closure called greet that takes a name parameter and returns a greeting string using special_number
Define a closure called greet using lambda that takes one parameter name and returns the string "Hello, #{name}! Your special number is #{special_number}.".
Ruby
Hint
Use lambda { |name| ... } to create the closure and use string interpolation with #{}.
3
Change special_number to 42 after defining greet
Change the value of special_number to 42 after the greet lambda is defined.
Ruby
Hint
Simply assign 42 to special_number after the lambda definition.
4
Call greet with "Alice" and "Bob" and print the results
Call the greet lambda with the argument "Alice" and print the result. Then call greet with "Bob" and print the result.
Ruby
Hint
Use puts greet.call("Alice") and puts greet.call("Bob") to print the greetings.
Practice
(1/5)
1. What is a closure in Ruby?
easy
A. A variable that changes its value automatically
B. A method that can only be called once
C. A function that remembers variables from its creation context
D. A class that cannot be inherited
Solution
Step 1: Understand the definition of closure
A closure is a function or block that keeps access to variables from where it was created, even if called later.
Step 2: Match the definition to options
A function that remembers variables from its creation context correctly describes this behavior. Other options describe unrelated concepts.
Final Answer:
A function that remembers variables from its creation context -> Option C
Quick Check:
Closure = function remembering variables [OK]
Hint: Closures remember variables where they were made [OK]
2. Which of the following is the correct way to create a closure using a lambda in Ruby?
easy
A. closure = lambda { |x| x * 2 }
B. closure = def(x) x * 2 end
C. closure = function(x) { x * 2 }
D. closure = -> x x * 2
Solution
Step 1: Identify Ruby lambda syntax
In Ruby, lambdas can be created with lambda { |args| ... } or ->(args) { ... }.
Step 2: Check each option
closure = lambda { |x| x * 2 } uses lambda { |x| x * 2 }, which is valid. closure = def(x) x * 2 end uses invalid syntax for a lambda. closure = function(x) { x * 2 } uses JavaScript-like syntax. closure = -> x x * 2 misses parentheses in the arrow syntax, which is invalid.