Complete the code to define an instance variable in the initialize method.
class Person def initialize(name) [1] = name end end
The @ symbol is used to define instance variables in Ruby. Here, @name stores the value for each object.
Complete the code to access the instance variable inside a method.
class Car def initialize(model) @model = model end def show_model puts [1] end end
@.To access an instance variable inside a method, use the @ symbol before the variable name.
Fix the error in the code by completing the instance variable assignment.
class Book def initialize(title) [1] = title end def title @title end end
The instance variable must be assigned with @title to be accessible in other methods.
Fill both blanks to create a method that updates and returns an instance variable.
class User def initialize(age) @age = age end def birthday @age [1] 1 return [2] end end
-= instead of +=.The += operator adds 1 to the instance variable @age. Then returning @age shows the updated value.
Fill all three blanks to define an instance variable, update it, and return it.
class Counter def initialize(start) [1] = start end def increment [2] [3] 1 return @count end end
-= instead of +=.@.Instance variable @count is assigned in initialize, incremented with +=, and returned.