From d08721eee2df162cd8f4005c26489fa3e79119fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Maluk?= <80332200+SebastianMaluk@users.noreply.github.com> Date: Mon, 12 Aug 2024 10:49:31 -0400 Subject: [PATCH] refactor: long method --- ruby/gilded_rose.rb | 68 ++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a49744..2c3355f5e6 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -7,50 +7,48 @@ def initialize(items) 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 - 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 + update_quality_for_non_legendary_item(item) + elsif item.quality < 50 + item.quality += 1 + backstage_passes_update(item) end if item.name != "Sulfuras, Hand of Ragnaros" - item.sell_in = item.sell_in - 1 + item.sell_in -= 1 end 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" + increase_quality_when_under_50(item) + elsif item.name == "Backstage passes to a TAFKAL80ETC concert" + item.quality = 0 else - if item.quality < 50 - item.quality = item.quality + 1 - end + update_quality_for_non_legendary_item(item) end end end end + + def backstage_passes_update(item) + if item.name == "Backstage passes to a TAFKAL80ETC concert" + increase_quality_when_under_50(item) if item.sell_in < 11 + increase_quality_when_under_50(item) if item.sell_in < 6 + end + end + + private + + def increase_quality_when_under_50(item) + if item.quality < 50 + item.quality += 1 + end + end + + def update_quality_for_non_legendary_item(item) + if item.quality > 0 + if item.name != "Sulfuras, Hand of Ragnaros" + item.quality -= 1 + end + end + end end class Item