A closure is a block, proc, or lambda that captures variables from its surrounding context, allowing it to access those variables even when executed outside that context.
Click to reveal answer
intermediate
How does variable binding work inside a Ruby closure?
Variables used inside a closure capture the variables from the surrounding scope by reference, so the closure sees their updated values even if the variables change later.
Click to reveal answer
intermediate
What is the difference between a Proc and a lambda in Ruby closures?
Both are closures, but lambdas check the number of arguments and return control to the calling method, while Procs are more lenient with arguments and return from the enclosing context.
Click to reveal answer
advanced
Explain with an example how a closure can remember a variable's value after the variable changes.
Example: x = 10 closure = -> { x } x = 20 closure.call # returns 20 because closure captures variable by reference, not by value.
Click to reveal answer
beginner
Why are closures useful in Ruby programming?
Closures let you write flexible code by capturing context, enabling callbacks, delayed execution, and maintaining state without global variables.
Click to reveal answer
What does a Ruby closure capture from its surrounding environment?
AOnly global variables
BVariables and their current values
COnly method names
DNothing, closures have no context
✗ Incorrect
Closures capture variables and their current values from the surrounding environment.
In Ruby, what happens if you change a variable after creating a closure that uses it?
AThe closure breaks and raises an error
BThe closure keeps the old value forever
CThe closure sees the updated variable value
DThe closure ignores the variable
✗ Incorrect
Ruby closures capture variables by reference, so they see the updated value when called.
Which Ruby closure type checks the number of arguments strictly?
ALambda
BProc
CBlock
DMethod
✗ Incorrect
Lambdas check the number of arguments strictly, unlike Procs.
What is a common use case for closures in Ruby?
ATo create global variables
BTo perform file I/O
CTo define classes
DTo delay execution and maintain state
✗ Incorrect
Closures are often used to delay execution and maintain state without global variables.
Which of these is NOT a closure in Ruby?
AClass
BProc
CLambda
DBlock
✗ Incorrect
Classes are not closures; blocks, procs, and lambdas are closures.
Explain what a closure is in Ruby and how it handles variable binding.
Think about how blocks or lambdas remember variables from outside.
You got /4 concepts.
Describe the difference between a Proc and a lambda in Ruby closures.
Focus on argument checking and return behavior.
You got /4 concepts.
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.