These methods let you read or change an object's variable from outside the object, even if it's private.
Instance_variable_get and set in Ruby
object.instance_variable_get(:@variable_name) object.instance_variable_set(:@variable_name, value)
Use a symbol with @ before the variable name, like :@name.
These methods bypass normal access rules, so use carefully.
person.instance_variable_get(:@age)
person.instance_variable_set(:@name, "Alice")obj.instance_variable_get(:@score) # returns nil if @score not setThis program creates a Dog object with name and age. It then reads the name and age using instance_variable_get. Next, it changes the age to 6 using instance_variable_set and prints the new age.
class Dog def initialize(name, age) @name = name @age = age end end dog = Dog.new("Buddy", 5) # Get instance variables puts dog.instance_variable_get(:@name) puts dog.instance_variable_get(:@age) # Set instance variable dog.instance_variable_set(:@age, 6) puts dog.instance_variable_get(:@age)
These methods can access variables even if there are no getter or setter methods.
Use symbols with the @ sign to name the variable.
Be careful: changing variables this way can break object rules or cause bugs.
instance_variable_get reads an object's variable by name.
instance_variable_set changes an object's variable by name.
They let you work with variables directly, even if they are private.
Practice
instance_variable_get do?Solution
Step 1: Understand the method purpose
instance_variable_getis used to access the value of an instance variable by passing its name as a symbol or string.Step 2: Compare with other options
It does not delete, create, or list variables; it only reads the value.Final Answer:
It reads the value of an instance variable by its name. -> Option AQuick Check:
instance_variable_get reads variable value [OK]
- Confusing get with set (changing value)
- Thinking it deletes variables
- Assuming it lists all variables
@name to "Alice" using instance_variable_set?Solution
Step 1: Understand the argument format
The first argument toinstance_variable_setmust be the instance variable name as a symbol or string, including the '@' prefix.Step 2: Check each option
A: object.instance_variable_set(:nam, "Alice") misses '@'. B: object.instance_variable_set("nam", "Alice") misses '@'. C: passes a variable instead of symbol/string. D: uses:@namesymbol correctly.Final Answer:
object.instance_variable_set(:@name, "Alice") -> Option DQuick Check:
Use symbol with '@' prefix for setting [OK]
- Omitting '@' in variable name
- Passing variable instead of symbol/string
- Using string without '@' prefix
class Person
def initialize(name)
@name = name
end
end
p = Person.new("Bob")
puts p.instance_variable_get(:@name)Solution
Step 1: Understand instance variable initialization
The@namevariable is set to "Bob" in the constructor.Step 2: Use instance_variable_get to read value
Callinginstance_variable_get(:@name)returns the value "Bob" stored in@name.Final Answer:
Bob -> Option AQuick Check:
instance_variable_get returns stored value [OK]
- Expecting variable name instead of value
- Confusing with nil if variable not set
- Thinking it raises error if variable exists
class Car
def initialize
@speed = 0
end
def set_speed(value)
instance_variable_set(:speed, value)
end
end
car = Car.new
car.set_speed(50)
puts car.instance_variable_get(:@speed)Solution
Step 1: Check instance_variable_set argument
The argument toinstance_variable_setmust include '@' prefix to identify the instance variable.Step 2: Identify the error
Here,:speedis missing '@', so Ruby will not find the variable and will create a new one without '@', causing unexpected behavior.Final Answer:
Missing '@' in instance_variable_set argument name. -> Option CQuick Check:
Always include '@' in variable name for instance_variable_set [OK]
- Omitting '@' in variable name
- Thinking instance_variable_set can't be used in methods
- Confusing symbol and string usage
Box with private instance variable @content, how can you change its value to "Books" from outside the class using instance_variable_set?Solution
Step 1: Understand private variable access
Private instance variables cannot be accessed or changed directly from outside the class using normal assignment.Step 2: Use instance_variable_set with correct name
Usinginstance_variable_setwith the symbol including '@' allows changing the private variable from outside.Step 3: Check options
box.instance_variable_set(:@content, "Books") correctly uses:@content. box.instance_variable_set(:conten, "Books") misses '@', box.@content = "Books" is invalid syntax, box.set_content("Books") assumes a setter method which may not exist.Final Answer:
box.instance_variable_set(:@content, "Books") -> Option BQuick Check:
Use instance_variable_set with '@' to change private vars [OK]
- Omitting '@' in variable name
- Trying direct assignment to private variable
- Assuming setter method exists
Ready to prove it?
Pass the test and this pattern moves unknown
