0
0
Rubyprogramming~10 mins

Instance_variable_get and set in Ruby - Interactive Code Practice

Choose your learning style9 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.