Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  1. mhutter revised this gist . 2 changed files with 5 additions and 5 deletions.
    View
    8 car.rb
    @@ -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
    View
    2  notifier.rb
    @@ -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
  2. mhutter created this gist .
    View
    14 car.rb
    @@ -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
    View
    6 notifier.rb
    @@ -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.