attr_reader :quality, :days_remaining def initialize(quality, days_remaining) @quality, @days_remaining = quality, days_remaining end def tick end end class Normal < Item def tick @days_remaining -‐= 1 return if @quality == 0 @quality -‐= 1 @quality -‐= 1 if @days_remaining <= 0 end end class Brie < Item def tick @days_remaining -‐= 1 return if @quality >= 50 @quality += 1 @quality += 1 if @days_remaining <= 0 end end class Backstage < Item def tick @days_remaining -‐= 1 return if @quality >= 50 return @quality = 0 if @days_remaining < 0 @quality += 1 @quality += 1 if @days_remaining < 10 @quality += 1 if @days_remaining < 5 end end DEFAULT_CLASS = Item SPECIALIZED_CLASSES = { 'normal' => Normal, 'Aged Brie' => Brie, 'Backstage passes to a TAFKAL80ETC concert' => Backstage } def self.for(name, quality, days_remaining) (SPECIALIZED_CLASSES[name] || DEFAULT_CLASS).new(quality, days_remainin end end small objects class GildedRose attr_reader :name, :quality, :days_remaining def initialize(name, quality, days_remaining) @name, @quality, @days_remaining = name, quality, days_remaining end def tick if @name != 'Aged Brie' && @name != 'Backstage passes to a TAFKAL80ETC if @quality > 0 if @name != 'Sulfuras, Hand of Ragnaros' @quality -‐= 1 end end else if @quality < 50 @quality += 1 if @name == 'Backstage passes to a TAFKAL80ETC concert' if @days_remaining < 11 if @quality < 50 @quality += 1 end end if @days_remaining < 6 if @quality < 50 @quality += 1 end end end end end if @name != 'Sulfuras, Hand of Ragnaros' @days_remaining -‐= 1 end if @days_remaining < 0 if @name != "Aged Brie" if @name != 'Backstage passes to a TAFKAL80ETC concert' if @quality > 0 if @name != 'Sulfuras, Hand of Ragnaros' @quality -‐= 1 end end else @quality = @quality -‐ @quality end else if @quality < 50 @quality += 1 end end end end end big conditional Friday, March 7, 14