0
0
Rubyprogramming~10 mins

Define_method for dynamic methods in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Define_method for dynamic methods
Start
Call define_method with name and block
Ruby creates method dynamically
Method can be called like normal
Method executes block code
End
This flow shows how Ruby's define_method creates a method dynamically and then allows calling it like a normal method.
Execution Sample
Ruby
class Greeter
  define_method(:hello) do |name|
    "Hello, #{name}!"
  end
end

puts Greeter.new.hello("Alice")
This code defines a method 'hello' dynamically that greets a person by name, then calls it.
Execution Table
StepActionEvaluationResult
1Call define_method(:hello) with blockMethod 'hello' is created dynamicallyMethod 'hello' added to Greeter
2Create Greeter instanceInstance created#<Greeter:0x000...>
3Call hello("Alice") on instanceBlock runs with name='Alice'"Hello, Alice!" returned
4puts prints returned stringOutput to consoleHello, Alice!
💡 Method call completed and output printed
Variable Tracker
VariableStartAfter define_methodAfter instance creationAfter method call
hello (method)undefineddefined as dynamic methodavailable on instanceexecutes block with argument
name (block param)undefinedundefinedundefined"Alice" during method call
Key Moments - 2 Insights
Why can we call 'hello' on the instance even though it was not defined with 'def'?
Because define_method dynamically creates the method and adds it to the class, making it callable like any normal method (see execution_table step 1 and 3).
What is the role of the block passed to define_method?
The block contains the code that runs when the dynamic method is called. It can take parameters and return values (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling hello("Alice")?
Anil returned
BMethod not found error
C"Hello, Alice!" returned
D"hello" string returned
💡 Hint
Check execution_table row 3 for the method call result
At which step is the method 'hello' added to the Greeter class?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
See execution_table row 1 where define_method is called
If we change the block to return "Hi, #{name}", what changes in the execution_table output?
AOutput changes to "Hi, Alice"
BMethod creation fails
CNo change in output
DError at method call
💡 Hint
Output depends on the block code executed in step 3
Concept Snapshot
define_method(:name) { |params| ... } creates a method dynamically.
The method can be called like a normal method.
The block defines the method body.
Useful for dynamic or metaprogramming tasks.
Allows passing parameters and returning values.
Full Transcript
This example shows how Ruby's define_method creates a method dynamically inside a class. The method 'hello' is defined with a block that takes a name and returns a greeting string. When we create an instance of Greeter and call hello with 'Alice', the block runs and returns 'Hello, Alice!'. The method behaves like any normal method even though it was created dynamically. This is useful when you want to create methods on the fly based on conditions or data.