Module_eval lets you add or change methods in a module while the program is running. This helps make your code flexible and adapt to new needs without rewriting it.
Module_eval for dynamic behavior in Ruby
module ModuleName module_eval do # method definitions or code here end end
You can pass a string or a block to module_eval.
Code inside module_eval runs as if it is inside the module.
hello to the Greeter module using a block.module Greeter module_eval do def hello "Hello!" end end end
bye to Greeter using a string.module Greeter module_eval "def bye; 'Goodbye!'; end" end
square inside MathOps dynamically.module MathOps module_eval do def square(x) x * x end end end
This program creates a module DynamicModule and adds a method greet dynamically using module_eval. Then a class Person includes this module. When we create a Person object and call greet, it prints a greeting.
module DynamicModule module_eval do def greet(name) "Hello, #{name}!" end end end class Person include DynamicModule end p = Person.new puts p.greet("Alice")
Using module_eval can be powerful but also risky if you run untrusted code.
It changes the module at runtime, so debugging can be harder if overused.
Prefer clear and simple code unless you really need dynamic behavior.
Module_eval lets you add or change methods inside a module while the program runs.
You can pass a block or a string to module_eval to define methods dynamically.
This helps make your code flexible but use it carefully to keep code readable and safe.
Practice
What does module_eval do in Ruby?
Solution
Step 1: Understand the purpose of
module_evalmodule_evallets you run code inside a module's context, allowing you to add or change methods dynamically.Step 2: Compare options with the definition
Only "It allows adding or changing methods inside a module dynamically." correctly describes this dynamic behavior. Other options describe unrelated actions.
Final Answer:
It allows adding or changing methods inside a module dynamically. -> Option DQuick Check:
module_eval adds or changes methods = A [OK]
- Thinking module_eval deletes methods
- Confusing module_eval with module creation
- Assuming module_eval converts modules to classes
Which of the following is the correct syntax to define a method greet inside a module Mod using module_eval?
module Mod # Your code here end
Solution
Step 1: Recall correct module_eval syntax
You can pass a block to
module_evalwith method definitions inside curly braces.Step 2: Check each option's syntax
Mod.module_eval { def greet; 'Hi'; end } uses a block correctly. Mod.module_eval def greet; 'Hi'; end misses braces. Mod.module_eval('def greet; 'Hi'; end') has mismatched quotes (inner single quote breaks the outer single-quoted string), causing syntax error. Mod.module_eval :def greet; 'Hi'; end uses invalid symbol syntax.
Final Answer:
Mod.module_eval { def greet; 'Hi'; end } -> Option AQuick Check:
Block syntax with braces is correct = A [OK]
- Omitting braces for block
- Using invalid symbol syntax
- Confusing string and block syntax
What will be the output of the following Ruby code?
module M
module_eval do
def hello
'Hello from M'
end
end
end
class C
include M
end
puts C.new.helloSolution
Step 1: Understand method definition inside module_eval
The methodhellois defined inside moduleMusingmodule_eval, so it becomes an instance method ofM.Step 2: Check class inclusion and method call
ClassCincludesM, so instances ofChavehellomethod. CallingC.new.helloreturns 'Hello from M'.Final Answer:
Hello from M -> Option AQuick Check:
module_eval defines method, include makes it accessible = D [OK]
- Assuming method is not defined
- Confusing module_eval with class_eval
- Expecting syntax error from block
Identify the error in the following code snippet:
module M module_eval 'def greet; puts "Hi" end' end class C include M end C.new.greet
Solution
Step 1: Analyze module_eval string syntax
The string passed tomodule_evaldefines methodgreetcorrectly with a semicolon separating statements.Step 2: Check method availability and call
ClassCincludesM, sogreetis available to instances. CallingC.new.greetprints 'Hi'.Final Answer:
No error; code runs and prints 'Hi'. -> Option BQuick Check:
module_eval string defines method correctly = C [OK]
- Thinking semicolon is missing
- Assuming greet is not instance method
- Expecting runtime error from string eval
You want to dynamically add multiple methods foo and bar to a module Mod using module_eval. Which approach correctly adds both methods so they can be used by classes including Mod?
Solution
Step 1: Understand multiple method definitions with module_eval
You can define multiple methods in one block or multiple times with separate calls tomodule_eval. Both ways add instance methods to the module.Step 2: Evaluate each option
Mod.module_eval do def foo; 'foo'; end def bar; 'bar'; end end defines both as instance methods (no self.) in one block, correct. All of the above "All of the above" is incorrect. Mod.module_eval do def self.foo; 'foo'; end end Mod.module_eval do def self.bar; 'bar'; end end uses separate blocks butdef self.adds singleton methods to Mod, not instance methods. Mod.module_eval('def self.foo; "foo"; end') Mod.module_eval('def self.bar; "bar"; end') uses strings withdef self., same issue - not accessible afterinclude.Final Answer:
Mod.module_eval do def foo; 'foo'; end def bar; 'bar'; end end -> Option CQuick Check:
Block with plain def (no self) adds instance methods = A [OK]
- Thinking only one method per module_eval is allowed
- Confusing string and block syntax
- Using self.def for instance methods (makes singletons)
Ready to prove it?
Pass the test and this pattern moves unknown
