1
+ # Problem: https://exercism.org/tracks/ruby/exercises/gilded-rose
2
+
3
+ # Solution
4
+ Item = Struct . new ( :name , :sell_in , :quality ) #instead of struct a class Item can be created
5
+
6
+ class GildedRose
7
+ REVERSE_AGING_ITEMS = [ "Aged Brie" , "Sulfuras, Hand of Ragnaros" , "Backstage passes to a TAFKAL80ETC concert" ]
8
+
9
+ def initialize ( items )
10
+ @items = items
11
+ end
12
+
13
+ def update!
14
+ @items . each do |item |
15
+ update_quality ( item )
16
+ update_sell_in ( item )
17
+ end
18
+ end
19
+
20
+ def update_quality ( item )
21
+ is_conjured = item . name . downcase . start_with? ( "conjured" )
22
+ if is_conjured
23
+ return update_conjured_item_quality ( item )
24
+ end
25
+
26
+ if REVERSE_AGING_ITEMS . include? ( item . name )
27
+ return update_reverse_aging ( item )
28
+ end
29
+
30
+ update_normal_item ( item , false )
31
+ end
32
+
33
+ def update_sell_in ( item )
34
+ item . sell_in -=1 unless item . name == "Sulfuras, Hand of Ragnaros"
35
+ end
36
+
37
+ def update_normal_item ( item , is_conjured = false )
38
+ sell_in_not_arrived = ( item . sell_in >0 )
39
+ if sell_in_not_arrived
40
+ item . quality -=1 if is_conjured
41
+ item . quality = item . quality >=1 ? item . quality -1 : 0
42
+ return item . quality
43
+ end
44
+ item . quality = item . quality >=2 ? item . quality -2 : 0
45
+ end
46
+
47
+ def update_reverse_aging ( item )
48
+ return if item . quality ==50 or item . name . include? ( "Sulfuras, Hand of Ragnaros" )
49
+ return update_backstage_passes ( item ) if item . name . include? ( "Backstage passes to a TAFKAL80ETC concert" )
50
+ item . quality = item . sell_in >0 ? item . quality +1 : item . quality +2
51
+ item . quality = item . quality > 50 ? 50 : item . quality
52
+ end
53
+
54
+ def update_backstage_passes ( item , is_conjured = false )
55
+ return item . quality = 0 if item . sell_in <=0
56
+ if item . sell_in <=5
57
+ item . quality +=3
58
+ elsif item . sell_in <=10
59
+ item . quality +=2
60
+ else
61
+ item . quality +=1
62
+ end
63
+ item . quality -=1 if is_conjured
64
+ item . quality = item . quality > 50 ? 50 : item . quality
65
+ end
66
+
67
+ def update_conjured_item_quality ( item )
68
+ return item . quality = 0 if item . sell_in <=0
69
+ if item . name . include? ( "backstage passes to a TAFKAL80ETC concert" )
70
+ return update_backstage_passes ( item , true )
71
+ end
72
+ return update_reverse_aging ( item ) if item . name . include? ( "Sulfuras" ) or item . name . include? ( "Aged Brie" )
73
+ return update_normal_item ( item , true )
74
+ end
75
+ end
0 commit comments