0
0
Rubyprogramming~10 mins

Module_eval for dynamic behavior in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Module_eval for dynamic behavior
Define Module
Call module_eval with code string
Evaluate code inside module context
Add methods or variables dynamically
Use new methods on module or including classes
End
This flow shows how module_eval runs code inside a module to add methods dynamically, then those methods can be used.
Execution Sample
Ruby
module Greeter
  module_eval do
    def hello
      "Hello!"
    end
  end
end

puts Greeter.new.hello rescue 'No instance method'
Defines a method dynamically inside a module using module_eval, then tries to call it on an instance.
Execution Table
StepActionEvaluationResult
1Define module GreeterModule Greeter createdGreeter module exists
2Call module_eval with block defining method helloMethod hello defined inside GreeterGreeter has method hello
3Try to call Greeter.new.helloGreeter.new raises NoMethodErrorRescue prints 'No instance method'
4Include Greeter in a class PersonPerson includes Greeter methodsPerson instances have hello method
5Create Person.new.hello callhello method returns 'Hello!'Output: Hello!
💡 Greeter module alone cannot be instantiated; methods added via module_eval are available when included in a class.
Variable Tracker
VariableStartAfter module_evalAfter include in PersonAfter Person.new
Greetermodule definedmethod hello addedunchangedunchanged
Personundefinedundefinedincludes Greeterinstance created
person_instanceundefinedundefinedundefinedobject with hello method
Key Moments - 2 Insights
Why does calling Greeter.new.hello fail even after defining hello with module_eval?
Because modules cannot be instantiated with .new. The method hello is defined in the module, but you must include the module in a class to use it on instances. See execution_table step 3 and 4.
How does module_eval add methods dynamically inside a module?
module_eval runs the given code block or string inside the module's context, so methods defined inside become part of the module. This is shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3 when calling Greeter.new.hello?
AIt successfully calls hello and prints 'Hello!'
BIt returns nil because hello is not defined
CIt raises an error because Greeter cannot be instantiated
DIt calls hello but returns an empty string
💡 Hint
Check execution_table row 3 where Greeter.new raises NoMethodError
At which step does the hello method become usable on an instance?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
See execution_table step 4 where Person includes Greeter
If we remove module_eval and define hello normally inside Greeter, what changes in the execution?
Ahello method is not added dynamically
BNo change; behavior is the same
CGreeter can be instantiated now
DPerson cannot include Greeter anymore
💡 Hint
module_eval allows dynamic code evaluation; normal definition is static (see concept_snapshot)
Concept Snapshot
module_eval runs code inside a module's context.
It can add methods dynamically.
Modules cannot be instantiated directly.
Include modules in classes to use added methods.
module_eval accepts strings or blocks.
Useful for dynamic behavior.
Full Transcript
This example shows how module_eval lets you add methods inside a module dynamically. First, the module Greeter is defined. Then module_eval runs a block that defines method hello inside Greeter. Trying to call Greeter.new.hello fails because modules cannot be instantiated. To use the method, we include Greeter in a class Person. Now Person instances can call hello and get 'Hello!'. This shows module_eval's power to add behavior dynamically inside modules, but you must include modules in classes to use instance methods.