About Instance Variables
Key Points:
- When a class'
.new method is called to create an object instance, the .initialize method is passed all arguments to initialize the instance's state.
- instance variable names are prefixed with
@.
- instance variables default to
nil until they are explicitly set.
- instance variables are private by default, and they should be manipulated with getters and setters
class Backpack
initialize(owner)
@owner = owner
end
def owner
@owner
end
def owner=(new_owner)
@owner = new_owner
end
end
- Methods named with a trailing
= are recognized as setters by Ruby, and allow the syntactic "sugar" use of the assignment syntax, e.g. Backpack.new("Sven").owner = "Ayah". Notice the space between owner and = while the actual method name is owner=.
- Getters and setters can be created using the
attr_reader, attr_writer, and attr_accessor methods:
-
attr_reader: Create getters for the symbols listed
-
attr_writer: Create setters for the symbols listed
-
attr_accessor: Create getters and setters for the symbols listed
class Backpack
attr_accessor :owner
initialize(owner)
@owner = owner
end
end
- Why use getters and setters rather than the instance variable directly?
- If there was a typographical error (we call these "typo") in the previous example (e.g.
@ownar), it would fail silently, potentially introducing a bug into the system.
- Getters and setters make this explicit, and will raise an error when a typo is made
References
Initializing object instances
Instance variables