0
0
Rubyprogramming~10 mins

Send for calling methods dynamically in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Send for calling methods dynamically
Start
Have object
Call send with method name as symbol/string
send looks up method
Method executed on object
Return method result
End
The send method takes a method name and calls that method on the object dynamically, returning the result.
Execution Sample
Ruby
class Greeter
  def hello
    "Hi!"
  end
end

obj = Greeter.new
puts obj.send(:hello)
This code calls the hello method dynamically on obj using send and prints the result.
Execution Table
StepActionEvaluationResult
1Create Greeter class with hello methodDefined class Greeter with method helloClass ready
2Create obj = Greeter.newobj is new Greeter instanceobj created
3Call obj.send(:hello)send looks for method :hello on objMethod found
4Execute hello methodhello returns "Hi!""Hi!" returned
5puts prints resultOutput to consoleHi! printed
💡 send completed method call and returned result
Variable Tracker
VariableStartAfter 1After 2After 3Final
objundefinedGreeter instanceGreeter instanceGreeter instanceGreeter instance
resultundefinedundefinedundefined"Hi!""Hi!"
Key Moments - 3 Insights
Why do we use a symbol :hello instead of a string "hello" in send?
send accepts both symbols and strings, but symbols are preferred for method names because they are more efficient and commonly used in Ruby. See execution_table step 3 where :hello is passed.
What happens if the method name passed to send does not exist?
Ruby raises a NoMethodError because send tries to call a method that is not defined on the object. This is not shown here but would stop execution after step 3.
Can send call private methods?
Yes, send can call private and protected methods, bypassing normal access control. This is a powerful feature but should be used carefully.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of obj.send(:hello) at step 4?
A"Hi!"
BNoMethodError
Cnil
D"hello"
💡 Hint
Check the 'Result' column at step 4 in execution_table
At which step does send look up the method on the object?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table for method lookup
If we change send(:hello) to send(:bye), what will happen?
AIt will print nil
BIt will print "Hi!" anyway
CIt will raise NoMethodError
DIt will call hello method
💡 Hint
Refer to key_moments about what happens if method does not exist
Concept Snapshot
Use send to call methods dynamically on an object.
Syntax: object.send(:method_name, args)
Method name can be symbol or string.
send bypasses access control (can call private methods).
Returns the method's result.
Raises error if method missing.
Full Transcript
This visual execution shows how Ruby's send method calls methods dynamically. First, a Greeter class with a hello method is defined. Then an object obj is created from Greeter. When obj.send(:hello) is called, send looks up the hello method on obj, executes it, and returns "Hi!". The puts prints this result. Variables obj and result change as the code runs. Beginners often wonder why symbols are used, what happens if the method is missing, and if send can call private methods. The quizzes check understanding of these steps and outcomes.