0
0
Rubyprogramming~20 mins

Define_method with closures in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Define_method Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.greet
Ruby
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
ANoMethodError: undefined method `greet' for #<Greeter:0x000...>
B"Hello, !"
C"Hello, Alice!"
DSyntaxError
Attempts:
2 left
💡 Hint
Think about how define_method captures instance variables in closures.
Predict Output
intermediate
2: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]
A[0, 1, 2]
B[2, 2, 2]
C[nil, nil, nil]
DNoMethodError
Attempts:
2 left
💡 Hint
Each define_singleton_method captures the current value of i during the loop.
🔧 Debug
advanced
2:00remaining
Why does this define_method not capture the variable as expected?
Consider this Ruby code:
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

What 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
Afoo\nbar
Bfoo\nfoo
Cbar\nbar
DNameError
Attempts:
2 left
💡 Hint
Think about how the block variable 'name' is captured in the closure.
📝 Syntax
advanced
2: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
end
Ruby
class Test
  def setup
    define_method(:hello) do
      puts "Hello"
    end
  end
end
Adefine_method(:hello) do puts "Hello" end
Bdefine_method(:hello) { puts "Hello" }
C
define_method(:hello) do
  puts "Hello"
end
Ddefine_method(:hello) puts "Hello" end
Attempts:
2 left
💡 Hint
Check the block syntax carefully.
🚀 Application
expert
3: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  # => 30
Ruby
class DynamicAttrs
  def initialize(attrs)
    @attrs = attrs
    # define getters here
  end
end

obj = DynamicAttrs.new({name: "Bob", age: 30})
obj.name
obj.age
A
def initialize(attrs)
  @attrs = attrs
  attrs.each do |key, value|
    define_singleton_method(key) { value }
  end
end
B
def initialize(attrs)
  @attrs = attrs
  attrs.each do |key, value|
    define_singleton_method(key) { @attrs[key] }
  end
end
C
def initialize(attrs)
  @attrs = attrs
  attrs.each do |key, value|
    define_method(key) { @attrs[key] }
  end
end
D
def initialize(attrs)
  @attrs = attrs
  attrs.each do |key, value|
    define_method(key) { value }
  end
end
Attempts:
2 left
💡 Hint
Consider where the methods are defined and how closures capture variables.