Challenge - 5 Problems
Define_method Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of define_method capturing variable
What is the output of this Ruby code?
class Greeter
def initialize(name)
@name = name
end
def create_greeting
define_method(:greet) do
"Hello, #{@name}!"
end
end
end
g = Greeter.new("Alice")
g.create_greeting
g.greetRuby
class Greeter def initialize(name) @name = name end def create_greeting define_method(:greet) do "Hello, #{@name}!" end end end g = Greeter.new("Alice") g.create_greeting g.greet
Attempts:
2 left
💡 Hint
Think about how define_method captures instance variables in closures.
✗ Incorrect
The define_method method is a private method of Module, so it cannot be called directly inside an instance method without using self.class or similar. Therefore, calling define_method inside an instance method without proper context will raise a NoMethodError when calling greet.
❓ Predict Output
intermediate2:00remaining
Closure behavior with loop variable in define_method
What is the output of this Ruby code?
class Counter
def initialize
@methods = []
3.times do |i|
define_singleton_method("count_#{i}") do
i
end
end
end
end
c = Counter.new
[c.count_0, c.count_1, c.count_2]Ruby
class Counter def initialize @methods = [] 3.times do |i| define_singleton_method("count_#{i}") do i end end end end c = Counter.new [c.count_0, c.count_1, c.count_2]
Attempts:
2 left
💡 Hint
Each define_singleton_method captures the current value of i during the loop.
✗ Incorrect
The block passed to define_singleton_method captures the current value of i for each iteration, so count_0 returns 0, count_1 returns 1, and count_2 returns 2.
🔧 Debug
advanced2:00remaining
Why does this define_method not capture the variable as expected?
Consider this Ruby code:
What is the output and why?
class Example
def create_methods
names = ["foo", "bar"]
names.each do |name|
define_method(name) do
name
end
end
end
end
ex = Example.new
ex.create_methods
puts ex.foo
puts ex.barWhat is the output and why?
Ruby
class Example def create_methods names = ["foo", "bar"] names.each do |name| define_method(name) do name end end end end ex = Example.new ex.create_methods puts ex.foo puts ex.bar
Attempts:
2 left
💡 Hint
Think about how the block variable 'name' is captured in the closure.
✗ Incorrect
The block variable 'name' is shared across iterations, so both methods return the last value 'bar'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in define_method usage
Which option contains a syntax error when defining a method with define_method?
class Test
def setup
define_method(:hello) do
puts "Hello"
end
end
endRuby
class Test def setup define_method(:hello) do puts "Hello" end end end
Attempts:
2 left
💡 Hint
Check the block syntax carefully.
✗ Incorrect
Option D is missing the 'do' keyword before the block, causing a syntax error.
🚀 Application
expert3:00remaining
Using define_method with closures to create dynamic attribute methods
You want to create a Ruby class that dynamically defines getter methods for a list of attributes using define_method and closures. Which option correctly defines these methods so that each getter returns the correct attribute value?
class DynamicAttrs
def initialize(attrs)
@attrs = attrs
# define getters here
end
end
obj = DynamicAttrs.new({name: "Bob", age: 30})
obj.name # => "Bob"
obj.age # => 30Ruby
class DynamicAttrs def initialize(attrs) @attrs = attrs # define getters here end end obj = DynamicAttrs.new({name: "Bob", age: 30}) obj.name obj.age
Attempts:
2 left
💡 Hint
Consider where the methods are defined and how closures capture variables.
✗ Incorrect
Option B defines singleton methods on the instance that access @attrs by key, ensuring correct values are returned dynamically.