Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created May 28, 2009 21:00
Show Gist options
  • Save sarahhodne/119568 to your computer and use it in GitHub Desktop.
Save sarahhodne/119568 to your computer and use it in GitHub Desktop.
class Battery
attr_reader :level
def initialize
@level = 0
end
def charge(uplevels)
@level += uplevels
@level = 100 if @level > 100
end
def use(downlevels)
@level -= downlevels
@level = 0 if @level < 0
end
end
class IPod
attr_reader :battery
def initialize
@battery = Battery.new
end
end
iPod = IPod.new
iPod.battery.level # => 0
iPod.battery.charge(80)
iPod.battery.level # => 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment