public
Last active

  • Download Gist
mhutter revised this gist . 2 changed files with 5 additions and 5 deletions. View gist @ 2e34e0e
car.rb
8 
... 1 2 3 4 5 6 7   8 9 10 11 12 13       14 ...   1   2 3 4   5 6 7 8   9 10 11 12 13 14
@@ -1,14 +1,14 @@
-require 'observer'
class Car
- include Observable
attr_reader :mileage, :service
def initialize(mileage = 0, service = 3000)
@mileage, @service = mileage, service
- add_observer(Notifier.new)
+ @observers = [Notifier.new]
end
def log(miles)
@mileage += miles
- changed
notify_observers(self, miles)
end
+ def notify_observers(*args)
+ @observers.each { |o| o.update(*args) }
+ end
end
notifier.rb
2 
... 1 2 3 4   5 6 ... 1 2 3   4 5 6
@@ -1,6 +1,6 @@
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
+ puts "The car needs to be taken in for a service!" if car.service <= car.mileage
end
end
mhutter created this gist . View gist @ a47caa9
car.rb
14 
...                             ... 1 2 3 4 5 6 7 8 9 10 11 12 13 14
@@ -0,0 +1,14 @@
+require 'observer'
+class Car
+ include Observable
+ attr_reader :mileage, :service
+ def initialize(mileage = 0, service = 3000)
+ @mileage, @service = mileage, service
+ add_observer(Notifier.new)
+ end
+ def log(miles)
+ @mileage += miles
+ changed
+ notify_observers(self, miles)
+ end
+end
notifier.rb
6 
...             ... 1 2 3 4 5 6
@@ -0,0 +1,6 @@
+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 &amp;lt;= car.mileage
+ end
+end
Something went wrong with that request. Please try again.