Skip to content

Instantly share code, notes, and snippets.

@JoshvaR88
Created March 9, 2016 08:00
Show Gist options
  • Save JoshvaR88/90a1069f0a307f18ea43 to your computer and use it in GitHub Desktop.
Save JoshvaR88/90a1069f0a307f18ea43 to your computer and use it in GitHub Desktop.
class Car
attr_reader :mileage, :service
def initialize(mileage = 0, service = 3000)
@mileage, @service = mileage, service
@observers = [Notifier.new]
end
def log(miles)
@mileage += miles
notify_observers(self, miles)
end
def notify_observers(*args)
@observers.each { |o| o.update(*args) }
end
end
class Notifier
def update(car, miles)
puts "The car has logged #{miles} miles, totaling #{car.mileage} miles traveled."
puts "The car needs to be taken in for a service!" if car.service <= car.mileage
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment