0
0
Rubyprogramming~20 mins

Module_eval for dynamic behavior in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Module_eval Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using module_eval?

Consider this Ruby code that uses module_eval to add a method dynamically. What will it print?

Ruby
module Greeter
  module_eval do
    def greet
      "Hello from module_eval!"
    end
  end
end

class Person
  include Greeter
end

puts Person.new.greet
ARuntimeError: wrong number of arguments (given 0, expected 1)
BNoMethodError: undefined method `greet' for #<Person:0x000...>
CHello from module_eval!
DSyntaxError: (eval):2: syntax error, unexpected keyword_def
Attempts:
2 left
💡 Hint

Think about how module_eval adds methods to the module's instance methods.

Predict Output
intermediate
2:00remaining
What error does this code raise when using module_eval incorrectly?

What error will this Ruby code raise?

Ruby
module M
  module_eval "def hello; 'Hi'; end"
  module_eval "def hello; 'Hi'"
end
ASyntaxError: (eval):2: syntax error, unexpected end-of-input, expecting `end`
BNoMethodError: undefined method `hello' for M:Module
CRuntimeError: can't redefine method
DNameError: undefined local variable or method `hello'
Attempts:
2 left
💡 Hint

Check the string passed to module_eval for proper method definition syntax.

🔧 Debug
advanced
2:00remaining
Why does this dynamically added method not work as expected?

Look at this Ruby code using module_eval. Why does calling obj.dynamic_method raise an error?

Ruby
module M
  module_eval do
    def dynamic_method
      'Dynamic!'
    end
  end
end

obj = Object.new
obj.extend(M)
puts obj.dynamic_method
ATypeError because <code>obj</code> is not a class
BSyntaxError due to incorrect use of module_eval block
CRuntimeError because <code>extend</code> cannot be used with modules modified by module_eval
DNoMethodError because <code>dynamic_method</code> is an instance method of M, but <code>obj</code> does not have it as a singleton method
Attempts:
2 left
💡 Hint

Think about how extend works and what methods it adds to an object.

📝 Syntax
advanced
2:00remaining
Which option correctly uses module_eval to define a class method?

Which code snippet correctly defines a class method info inside module M using module_eval?

A
module M
  module_eval do
    def self.info
      'Class method'
    end
  end
end
B
module M
  module_eval do
    def self.info
      'Class method'
    end
  end
end

class C
  extend M
end

puts C.info
C
module M
  module_eval do
    def info
      'Class method'
    end
  end
end

class &lt;&lt; self
  include M
end
D
module M
  module_eval "def self.info; 'Class method'; end"
end
Attempts:
2 left
💡 Hint

Remember that def self.method inside a module defines a method on the module object, not on classes including it.

🚀 Application
expert
3:00remaining
How to dynamically add multiple methods with module_eval from a hash?

You want to add multiple instance methods to a module dynamically using module_eval and a hash of method names and return values. Which code correctly does this?

Ruby
methods = { greet: 'Hello', farewell: 'Goodbye' }

module M
  # Add methods here
end

class Person
  include M
end

p = Person.new
puts p.greet
puts p.farewell
A
methods.each do |name, val|
  M.module_eval "def #{name}; '#{val}'; end"
end
B
methods.each do |name, val|
  M.module_eval do
    define_method(name) { val }
  end
end
C
methods.each do |name, val|
  M.module_eval do
    def name
      val
    end
  end
end
D
methods.each do |name, val|
  M.module_eval "define_method(:#{name}) { '#{val}' }"
end
Attempts:
2 left
💡 Hint

Think about how module_eval executes strings as code and how to interpolate method names and values correctly.