Skip to content

Instantly share code, notes, and snippets.

@rxbynerd
Created March 14, 2012 11:20
Show Gist options
  • Save rxbynerd/2035845 to your computer and use it in GitHub Desktop.
Save rxbynerd/2035845 to your computer and use it in GitHub Desktop.
class LightBulb
attr_accessor :state, :fitting, :filament, :wattage, :voltage
def initialize(wattage, fitting)
@wattage = wattage
@fitting = fitting
puts "creating a light bulb with #{wattage}W with a fitting of '#{fitting}'"
end
def is_on?
state == "on" ? true : false
end
def brightness
wattage
end
def toggle
puts "LightBulb#toggle #turning lightbulb on!"
@state = "on"
end
end
class EnergySavingLightBulb < LightBulb
def initalize(wattage, fitting)
@filament = "fluorescent"
@wattage = wattage
super(wattage, fitting)
end
def brightness
wattage * 2
end
end
class InvalidBulbException < StandardError; end
class Lamp
#attr_reader :bulb
def initalize(fitting)
@fitting = fitting
end
def mount(bulb)
if @fitting == bulb.fitting
@bulb = bulb
return false
else
return true
end
end
end
@bulb = EnergySavingLightBulb.new(25, "screw")
# puts "@bulb is on? #=> #{@bulb.is_on?}"
# puts "turning the lightbulb on!"
@bulb.toggle
# puts "@bulb is on? #=> #{@bulb.is_on?}"
@lamp = Lamp.new("screw")
@lamp.mount @bulb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment