Objects can hold their own state by setting instance variables. These variables live within an object and are normally used to store some internal state.
Instance variables always start with an
You can create a new instance variable by setting a variable starting with an
class Repeater def hear(something) # Save what's been heard into a new instance variable called @heard @heard = something end def repeat # Return the value of the instance variable @heard end end repeater = Repeater.new repeater.hear("Hello, friend!") print repeater.repeat # Prints "Hello, friend!"
Objects usually set their initial state in an
initialize method, which is automatically called when calling
new on a class.
class Airplane def initialize @wings = 2 end def wings @wings end end plane = Airplane.new plane.wings # returns 2
initialize method may also take arguments, so that each instance can start with a custom state:
class Suitcase def initialize(locked) @locked = locked end end
It is good practice to consider instance variables to be private from external read and writes. We instead define methods to get and set their values. For example:
class Suitcase def initialize @locked = false end def locked? # Query methods should be named with a trailing `?` @locked end def lock! # Methods which mutate state should have trailing `!` @locked = true end end suitcase = Suitcase.new suitcase.locked? # false suitcase.lock! suitcase.locked? # true
Nil is an English word meaning "nothing" or "zero".
nil is used to express the absence of an object.
In other programming languages,
none values may play a similar role.
# I do not have a favorite color favorite_color = nil
Ruby gives any instance variable the default value of
nil when it is first encountered, until it is otherwise given a value.
print @favourite_color # prints nil @favourite_color = "blue" print @favourite_color # prints "blue"
Working with an amusement park, you've been handed a specification to design a system to administer attendance and rides. You've been tasked with modeling the Attendee (person visiting the park).
Attendee#initialize method of the
Attendee class, it should take a height (in centimeters) and store it as an instance variable
Attendee.new(106) # => #<Attendee:0x000055c33e6c7e18 @height=106>
Attendee#height getter of the
Attendee class, it should return the instances height
Attendee.new(106).height # => 106
Not all attendees have bought a ride pass, but we need to know if they have a pass or not. Implement the
Attendee#pass_id getter for the
Attendee class, it should return the instance's pass_id or
nil if the Attendee doesn't have one.
Attendee.new(106).pass_id # => nil
Attendee#issue_pass! to mutate the state of the instance, and set the pass id instance variable to the argument. It should return the pass id.
attendee = Attendee.new(106) attendee.issue_pass!(42) attendee.pass_id # => 42
Some guests break the rules with unsafe behavior, so the park wants to be able to revoke passes. Implement
Attendee#revoke_pass! to mutate the state of the instance, and set the pass id to
attendee = Attendee.new(106) attendee.issue_pass!(42) attendee.revoke_pass! attendee.pass_id # => nil