質問編集履歴

2

回答者からコードはリンクではなく質問文内に記載したほうが良いとのアドバイスがあったため。。

2018/09/27 10:22

投稿

MasakiSakamoto
MasakiSakamoto

スコア13

test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,181 @@
13
13
 
14
14
 
15
15
  構文エラー箇所もわかればお伝えいただけると助かります。
16
+
17
+
18
+
19
+ ```Ruby
20
+
21
+ class Drink
22
+
23
+ attr_reader :name, :price
24
+
25
+
26
+
27
+ def self.coke
28
+
29
+ self.new 120, :coke
30
+
31
+ end
32
+
33
+
34
+
35
+ def self.redbull
36
+
37
+ self.new 200, :redbull
38
+
39
+ end
40
+
41
+
42
+
43
+ def self.water
44
+
45
+ self.new 100, :water
46
+
47
+ end
48
+
49
+
50
+
51
+ def initialize price, name
52
+
53
+ @name = name
54
+
55
+ @price = price
56
+
57
+ end
58
+
59
+
60
+
61
+ def ==(another)
62
+
63
+ self.name == another.name
64
+
65
+ end
66
+
67
+
68
+
69
+ def eql?(another)
70
+
71
+ self == another
72
+
73
+ end
74
+
75
+
76
+
77
+ def hash
78
+
79
+ name.hash
80
+
81
+ end
82
+
83
+
84
+
85
+ def to_s
86
+
87
+ "<Drink: name=#{name}, price=#{price}>"
88
+
89
+ end
90
+
91
+ end
92
+
93
+ ```
94
+
95
+ ```Ruby
96
+
97
+ class VendingMachine
98
+
99
+ USEABLE_MONEY = [10, 50, 100, 500, 1000]
100
+
101
+ attr_reader :total, :sale_amount
102
+
103
+
104
+
105
+ def initialize
106
+
107
+ @total = 0
108
+
109
+ @sale_amount = 0
110
+
111
+ @drink_table = {}
112
+
113
+ 5.times { store Drink.coke }
114
+
115
+ end
116
+
117
+
118
+
119
+ def insert(money)
120
+
121
+ USEABLE_MONEY.include?(money) ? nil.tap{@total += money } : money
122
+
123
+ end
124
+
125
+
126
+
127
+ def change
128
+
129
+ total.tap { @total = 0}
130
+
131
+ end
132
+
133
+
134
+
135
+ def store(drink)
136
+
137
+ nil.tap do
138
+
139
+ @drink_table[drink.name] = {price: drink.price, drinks: [] } unless @drink_table.has_key? drink.name
140
+
141
+ @drink_table[drink.name][:drinks] << drink
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+
149
+ def purchase(drink_name)
150
+
151
+ if purchaseable? drink_name
152
+
153
+ drink = @drink[drink_name][:drinks].pop
154
+
155
+ @sale_amount += drink.price
156
+
157
+ @total -= drink.price
158
+
159
+ [drink,change]
160
+
161
+ end
162
+
163
+ end
164
+
165
+
166
+
167
+ def purchaseable?(drink_name)
168
+
169
+ purchaseable_drink_names.include? drink_name]
170
+
171
+ end
172
+
173
+
174
+
175
+ def purchaseable_drink_names
176
+
177
+ @drink_table.select{|_, info| info[:price] <= total && info[:drinks].any? }.keys
178
+
179
+ end
180
+
181
+
182
+
183
+ def stock_info
184
+
185
+ Hash[@drink_table.map {|name, info| [name, { price: info[:price], stock: info[:drinks].size }] }]
186
+
187
+ end
188
+
189
+
190
+
191
+ end
192
+
193
+ ```

1

質問タイトルの変更。

2018/09/27 10:22

投稿

MasakiSakamoto
MasakiSakamoto

スコア13

test CHANGED
@@ -1 +1 @@
1
- rubyhoge.rbファイルを実行するとができない。
1
+ ターミナル上で【ruby hoge.rbを実行しても結果が返ってこない。
test CHANGED
File without changes