0
0
Rubyprogramming~10 mins

Class instance variables as alternative in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class instance variables as alternative
Define Class
Create Class Instance
Access Instance Variables
Use Class Instance Variables
Modify Class Instance Variables
Observe Changes in Instances
This flow shows how class instance variables are defined and accessed as an alternative to class variables, affecting all instances of the class.
Execution Sample
Ruby
class Example
  @count = 0

  def self.count
    @count
  end

  def initialize
    self.class.instance_variable_set(:@count, self.class.count + 1)
  end
end

Example.new
Example.new
puts Example.count
This code uses a class instance variable @count to track how many instances of Example are created.
Execution Table
StepActionClass Instance Variable @countInstance CreatedOutput
1Class Example defined, @count set to 00No
2First Example.new called, @count incremented1Yes
3Second Example.new called, @count incremented2Yes
4puts Example.count called2No2
5Execution ends2No
💡 No more code to execute, program ends
Variable Tracker
VariableStartAfter 1After 2Final
@count (class instance variable)0122
Key Moments - 3 Insights
Why does @count belong to the class and not to instances?
Because @count is defined with @ at the class level, it is a class instance variable, not an instance variable of objects. See execution_table step 1 where @count is set before any instance is created.
How does the initialize method update the class instance variable?
Inside initialize, self.class.instance_variable_set updates @count on the class itself, not on the instance. This is shown in execution_table steps 2 and 3 where @count increments.
Why does puts Example.count output 2?
Because @count was incremented twice when two instances were created, so Example.count returns 2 as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of @count after the first instance is created?
A2
B0
C1
DUndefined
💡 Hint
Check execution_table row 2 under 'Class Instance Variable @count'
At which step does the class instance variable @count get incremented the second time?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 for the second increment
If we create a third instance, what will Example.count return?
A2
B3
C1
D0
💡 Hint
Refer to variable_tracker showing @count increments with each new instance
Concept Snapshot
Class instance variables start with @ but belong to the class itself.
They are different from class variables (@@) and instance variables of objects.
Use self.class.instance_variable_set and instance_variable_get to modify/access.
Good for storing data shared by the class but not by subclasses.
Example: @count tracks number of instances created.
Access with class methods like self.count.
Full Transcript
This visual execution shows how class instance variables work in Ruby as an alternative to class variables. The class Example defines @count at the class level starting at 0. Each time a new instance is created, the initialize method increments @count by accessing it on the class itself. The execution table traces each step: defining the class, creating instances, updating @count, and finally printing the count. The variable tracker shows @count changing from 0 to 2 after two instances. Key moments clarify why @count belongs to the class, how initialize updates it, and why the final output is 2. The quiz tests understanding of these steps and the effect of creating more instances. This helps beginners see how class instance variables hold data shared by the class but separate from instance variables of objects.