0
0
Rubyprogramming~10 mins

Instance methods in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Instance methods
Create Object
Call Instance Method
Method Uses Object's Data
Return or Print Result
End
An instance method is called on an object and can use that object's data to perform actions or return values.
Execution Sample
Ruby
class Dog
  def bark
    "Woof!"
  end
end

dog = Dog.new
puts dog.bark
This code creates a Dog object and calls its instance method bark, which returns 'Woof!'.
Execution Table
StepActionObject StateMethod CalledOutput
1Define class Dog with method barkNo objects yetNoneNone
2Create dog object with Dog.newdog: instance of DogNoneNone
3Call dog.barkdog: instance of Dogbark"Woof!"
4Print output of dog.barkdog: instance of DogNoneWoof!
5End of programdog: instance of DogNoneNone
💡 Program ends after printing 'Woof!' from dog.bark method
Variable Tracker
VariableStartAfter Step 2After Step 3Final
dogundefinedDog instance createdDog instance unchangedDog instance unchanged
Key Moments - 2 Insights
Why do we call bark on dog and not on Dog class?
Instance methods belong to objects, not the class itself. The execution_table row 3 shows dog.bark calls bark on the dog object.
What does the bark method return?
The bark method returns the string "Woof!" as shown in execution_table row 3 and printed in row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when dog.bark is called at step 3?
Adog object
Bnil
C"Woof!"
DError
💡 Hint
Check the Output column at step 3 in the execution_table
At which step is the dog object created?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the Action and Object State columns in the execution_table
If we add a method that uses dog’s data, when would that method be called?
AAfter calling dog.bark
BOnly on the dog object after it is created
CBefore creating the dog object
DOn the Dog class itself
💡 Hint
Instance methods work on objects, see concept_flow and execution_table steps
Concept Snapshot
Instance methods belong to objects.
Call them on an object, not the class.
They can use the object's data.
Syntax: def method_name inside class.
Call: object.method_name
Returns value or performs action.
Full Transcript
This example shows how instance methods work in Ruby. First, a class Dog is defined with an instance method bark that returns "Woof!". Then, a dog object is created using Dog.new. The instance method bark is called on this dog object, returning the string "Woof!" which is then printed. Instance methods belong to objects, so you call them on an instance like dog, not on the class Dog itself. The execution table traces each step: defining the class, creating the object, calling the method, printing the output, and ending the program. The variable tracker shows the dog variable starting undefined, then holding the Dog instance after creation, and unchanged after method calls. Key moments clarify why methods are called on objects and what the method returns. The visual quiz tests understanding of these steps and concepts. This helps beginners see how instance methods run step-by-step.