Bird
Raised Fist0
Rubyprogramming~10 mins

Instance_variable_get and set in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the value of the instance variable @name.

Ruby
class Person
  def initialize(name)
    @name = name
  end

  def get_name
    [1]
  end
end

person = Person.new("Alice")
puts person.get_name
Drag options to blanks, or click blank then click option'
A@name = name
Binstance_variable_set(:@name)
Cinstance_variable_get(:@name)
Dget_instance_variable(:@name)
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance_variable_set instead of instance_variable_get.
Trying to access @name directly inside another method without returning it.
2fill in blank
medium

Complete the code to set the value of the instance variable @age to 30.

Ruby
class Person
  def initialize(age)
    [1]
  end

  def get_age
    instance_variable_get(:@age)
  end
end

person = Person.new(30)
puts person.get_age
Drag options to blanks, or click blank then click option'
Ainstance_variable_get(:@age)
Binstance_variable_set(:@age, age)
C@age = age
Dset_instance_variable(:@age, age)
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance_variable_get instead of instance_variable_set.
Assigning @age directly without using the method.
3fill in blank
hard

Fix the error in the code to correctly set the instance variable @city to 'Paris'.

Ruby
class Location
  def initialize(city)
    [1]
  end

  def get_city
    instance_variable_get(:@city)
  end
end

loc = Location.new("Paris")
puts loc.get_city
Drag options to blanks, or click blank then click option'
Ainstance_variable_set(:@city, city)
Binstance_variable_get(:@city, city)
Cinstance_variable_set(:city, city)
D@city = city
Attempts:
3 left
💡 Hint
Common Mistakes
Missing '@' in the symbol for the variable name.
Using instance_variable_get instead of instance_variable_set.
4fill in blank
hard

Fill both blanks to create a hash of instance variable names and their values for variables with names longer than 3 characters.

Ruby
class Data
  def initialize
    @id = 1
    @name = "Data"
    @val = 100
  end

  def long_vars
    vars = instance_variables.select { |var| var.to_s.[1] 3 }
    { var => instance_variable_get(var) [2] var in vars }
  end
end

data = Data.new
puts data.long_vars
Drag options to blanks, or click blank then click option'
Alength >
Blength <
Cfor
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for length comparison.
Using 'if' instead of 'for' in the hash comprehension.
5fill in blank
hard

Fill all three blanks to set an instance variable dynamically, get its value, and print it.

Ruby
class DynamicVar
  def set_var(name, value)
    [1]
  end

  def get_var(name)
    [2]
  end
end

obj = DynamicVar.new
obj.set_var(:@color, "blue")
puts [3]
Drag options to blanks, or click blank then click option'
Ainstance_variable_set(name, value)
Binstance_variable_get(name)
Cobj.instance_variable_get(:@color)
Dobj.instance_variable_set(:@color, "blue")
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance_variable_get to set a variable.
Trying to print the variable without calling instance_variable_get.

Practice

(1/5)
1. What does the Ruby method instance_variable_get do?
easy
A. It reads the value of an instance variable by its name.
B. It deletes an instance variable from an object.
C. It creates a new instance variable with a given name.
D. It lists all instance variables of an object.

Solution

  1. Step 1: Understand the method purpose

    instance_variable_get is used to access the value of an instance variable by passing its name as a symbol or string.
  2. Step 2: Compare with other options

    It does not delete, create, or list variables; it only reads the value.
  3. Final Answer:

    It reads the value of an instance variable by its name. -> Option A
  4. Quick Check:

    instance_variable_get reads variable value [OK]
Hint: Remember: get = read value of variable [OK]
Common Mistakes:
  • Confusing get with set (changing value)
  • Thinking it deletes variables
  • Assuming it lists all variables
2. Which of the following is the correct syntax to set an instance variable @name to "Alice" using instance_variable_set?
easy
A. object.instance_variable_set(:nam, "Alice")
B. object.instance_variable_set("nam", "Alice")
C. object.instance_variable_set(@name, "Alice")
D. object.instance_variable_set(:@name, "Alice")

Solution

  1. Step 1: Understand the argument format

    The first argument to instance_variable_set must be the instance variable name as a symbol or string, including the '@' prefix.
  2. 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 :@name symbol correctly.
  3. Final Answer:

    object.instance_variable_set(:@name, "Alice") -> Option D
  4. Quick Check:

    Use symbol with '@' prefix for setting [OK]
Hint: Use symbol with '@' prefix to set variable [OK]
Common Mistakes:
  • Omitting '@' in variable name
  • Passing variable instead of symbol/string
  • Using string without '@' prefix
3. What will be the output of this Ruby code?
class Person
  def initialize(name)
    @name = name
  end
end

p = Person.new("Bob")
puts p.instance_variable_get(:@name)
medium
A. Bob
B. nil
C. @name
D. Error

Solution

  1. Step 1: Understand instance variable initialization

    The @name variable is set to "Bob" in the constructor.
  2. Step 2: Use instance_variable_get to read value

    Calling instance_variable_get(:@name) returns the value "Bob" stored in @name.
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    instance_variable_get returns stored value [OK]
Hint: instance_variable_get returns actual stored value [OK]
Common Mistakes:
  • Expecting variable name instead of value
  • Confusing with nil if variable not set
  • Thinking it raises error if variable exists
4. Identify the issue in this Ruby code snippet:
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)
medium
A. The variable @speed is not initialized.
B. Cannot call instance_variable_set inside a method.
C. Missing '@' in instance_variable_set argument name.
D. instance_variable_get should use string, not symbol.

Solution

  1. Step 1: Check instance_variable_set argument

    The argument to instance_variable_set must include '@' prefix to identify the instance variable.
  2. Step 2: Identify the error

    Here, :speed is missing '@', so Ruby will not find the variable and will create a new one without '@', causing unexpected behavior.
  3. Final Answer:

    Missing '@' in instance_variable_set argument name. -> Option C
  4. Quick Check:

    Always include '@' in variable name for instance_variable_set [OK]
Hint: Always prefix variable name with '@' in set/get methods [OK]
Common Mistakes:
  • Omitting '@' in variable name
  • Thinking instance_variable_set can't be used in methods
  • Confusing symbol and string usage
5. Given a class Box with private instance variable @content, how can you change its value to "Books" from outside the class using instance_variable_set?
hard
A. box.instance_variable_set(:conten, "Books")
B. box.instance_variable_set(:@content, "Books")
C. box.@content = "Books"
D. box.set_content("Books")

Solution

  1. Step 1: Understand private variable access

    Private instance variables cannot be accessed or changed directly from outside the class using normal assignment.
  2. Step 2: Use instance_variable_set with correct name

    Using instance_variable_set with the symbol including '@' allows changing the private variable from outside.
  3. 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.
  4. Final Answer:

    box.instance_variable_set(:@content, "Books") -> Option B
  5. Quick Check:

    Use instance_variable_set with '@' to change private vars [OK]
Hint: Use '@' prefix symbol to set private variables externally [OK]
Common Mistakes:
  • Omitting '@' in variable name
  • Trying direct assignment to private variable
  • Assuming setter method exists
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard