diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..436b0db7 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -1,56 +1,49 @@ class GildedRose - def initialize(items) @items = items end def update_quality() @items.each do |item| - if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" - if item.quality > 0 - if item.name != "Sulfuras, Hand of Ragnaros" - item.quality = item.quality - 1 - end - end + next if item.name == "Sulfuras, Hand of Ragnaros" + + if item.name == "Aged Brie" + item.quality += 1 if item.quality < 50 + elsif item.name == "Backstage passes to a TAFKAL80ETC concert" + increases_proportionally_to_age(item) else - if item.quality < 50 - item.quality = item.quality + 1 - if item.name == "Backstage passes to a TAFKAL80ETC concert" - if item.sell_in < 11 - if item.quality < 50 - item.quality = item.quality + 1 - end - end - if item.sell_in < 6 - if item.quality < 50 - item.quality = item.quality + 1 - end - end - end - end - end - if item.name != "Sulfuras, Hand of Ragnaros" - item.sell_in = item.sell_in - 1 + decrease_quality(item) end + + item.sell_in -= 1 if item.name != "Sulfuras, Hand of Ragnaros" + if item.sell_in < 0 - if item.name != "Aged Brie" - if item.name != "Backstage passes to a TAFKAL80ETC concert" - if item.quality > 0 - if item.name != "Sulfuras, Hand of Ragnaros" - item.quality = item.quality - 1 - end - end - else - item.quality = item.quality - item.quality - end + if item.name == "Aged Brie" + item.quality += 1 if item.quality < 50 + elsif item.name == "Backstage passes to a TAFKAL80ETC concert" + item.quality = 0 else - if item.quality < 50 - item.quality = item.quality + 1 - end + decrease_quality(item) end end end end + + private + + def decrease_quality(item) + decrement = item.name == "Conjured" ? 2 : 1 + item.quality -= decrement if item.quality > 0 + end + + def increases_proportionally_to_age(item) + if item.sell_in <= 10 + item.quality += 1 if item.quality < 50 + end + if item.sell_in <= 5 + item.quality += 1 if item.quality < 50 + end + end end class Item