0
0
Rubyprogramming~10 mins

Instance_variable_get and set in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Instance_variable_get and set
Create Object
Set Instance Variable
Get Instance Variable
Use or Display Value
End
This flow shows creating an object, setting an instance variable, getting its value, and using it.
Execution Sample
Ruby
class Person
  def initialize(name)
    @name = name
  end
end

p = Person.new("Alice")
p.instance_variable_get(:@name)
Creates a Person object with a name and gets the value of the @name instance variable.
Execution Table
StepActionCode/MethodInstance Variable StateOutput
1Create Person objectPerson.new("Alice")@name = "Alice"Person object created
2Initialize @nameinitialize("Alice")@name = "Alice"None
3Get @name valueinstance_variable_get(:@name)@name = "Alice""Alice"
4Set @age valueinstance_variable_set(:@age, 30)@name = "Alice", @age = 3030
5Get @age valueinstance_variable_get(:@age)@name = "Alice", @age = 3030
6Get non-existing @heightinstance_variable_get(:@height)@name = "Alice", @age = 30nil
7End-@name = "Alice", @age = 30Execution ends
💡 All instance variables set or retrieved; no more actions.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
@namenil"Alice""Alice""Alice"
@agenilnil3030
@heightnilnilnilnil
Key Moments - 3 Insights
Why does instance_variable_get return nil for @height?
Because @height was never set on the object, instance_variable_get returns nil as shown in step 6 of the execution_table.
Can instance_variable_set create a new instance variable?
Yes, instance_variable_set creates or updates the variable. Step 4 shows @age being created and set to 30.
Does instance_variable_get require the variable name as a symbol or string?
It accepts the variable name as a symbol or string starting with @, like :@name, as used in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 3?
Anil
B30
C"Alice"
DError
💡 Hint
Check the Output column for step 3 in the execution_table.
At which step is the @age instance variable created and set?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the Instance Variable State column to see when @age appears.
If you call instance_variable_get(:@height) before setting it, what will be returned?
AAn error
Bnil
C"height"
D0
💡 Hint
Refer to step 6 in the execution_table and the variable_tracker row for @height.
Concept Snapshot
Instance_variable_get(:@var) returns the value of @var or nil if unset.
Instance_variable_set(:@var, value) sets or creates @var with value.
Use symbols or strings with @ prefix for variable names.
Useful to access or change variables dynamically.
Does not call getter/setter methods, works directly on variables.
Full Transcript
This visual execution shows how Ruby's instance_variable_get and instance_variable_set methods work. First, a Person object is created with @name set to "Alice". Using instance_variable_get(:@name) returns "Alice". Then, instance_variable_set(:@age, 30) creates a new instance variable @age with value 30. Getting @age returns 30. Trying to get an unset variable like @height returns nil. These methods let you read or write instance variables dynamically by name, without using normal getter or setter methods.