Consider this Ruby code that uses module_eval to add a method dynamically. What will it print?
module Greeter module_eval do def greet "Hello from module_eval!" end end end class Person include Greeter end puts Person.new.greet
Think about how module_eval adds methods to the module's instance methods.
The module_eval block defines an instance method greet inside the Greeter module. When Person includes Greeter, it gains this method. So calling Person.new.greet prints the string.
What error will this Ruby code raise?
module M module_eval "def hello; 'Hi'; end" module_eval "def hello; 'Hi'" end
Check the string passed to module_eval for proper method definition syntax.
The second module_eval call tries to define a method without the closing end. This causes a SyntaxError because Ruby expects the method definition to be properly closed.
Look at this Ruby code using module_eval. Why does calling obj.dynamic_method raise an error?
module M module_eval do def dynamic_method 'Dynamic!' end end end obj = Object.new obj.extend(M) puts obj.dynamic_method
Think about how extend works and what methods it adds to an object.
The method dynamic_method is defined as an instance method of module M. When obj.extend(M) is called, dynamic_method becomes a singleton method of obj. So calling obj.dynamic_method works fine and prints 'Dynamic!'. The error would occur if obj did not extend M. Here, the code runs correctly, so the error is a trick. The correct answer is that no error occurs, but since the question asks why it would raise an error, the only plausible error is NoMethodError if extend was missing.
Which code snippet correctly defines a class method info inside module M using module_eval?
Remember that def self.method inside a module defines a method on the module object, not on classes including it.
Options A and B define self.info inside the module, which becomes a method on the module object M itself, not on classes including it. Option B tries to include M into the singleton class of self, which is invalid syntax here. Option B defines self.info inside M and then extends class C with M, making info a class method of C. This works correctly.
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?
methods = { greet: 'Hello', farewell: 'Goodbye' }
module M
# Add methods here
end
class Person
include M
end
p = Person.new
puts p.greet
puts p.farewellThink about how module_eval executes strings as code and how to interpolate method names and values correctly.
Option A correctly uses string interpolation to define methods dynamically inside module_eval. Option A tries to use define_method inside a module_eval block, but module_eval with a block does not accept define_method like that. Option A tries to call define_method inside a string passed to module_eval, which is invalid syntax. Option A tries to define a method named literally name returning val variables, which are not accessible inside the method body, causing errors.