Class: Pebbles::AntiHangover
- Inherits:
-
Object
- Object
- Pebbles::AntiHangover
- Defined in:
- lib/pebbles/anti_hangover.rb
Constant Summary collapse
- VERSION =
'0.1.0'
- ALCOHOL_TYPE =
{ beer: { name: 'ビール', glass: 'レギュラー缶', unit: '本', alcoholicity: 5, net: 350, }, chuhi: { name: '酎ハイ', glass: 'サワーグラス', unit: '杯', alcoholicity: 7, net: 200, }, sake: { name: '日本酒', glass: '小徳利', unit: '本', alcoholicity: 12, net: 180, }, wine: { name: 'ワイン', glass: 'ワイングラス', unit: '杯', alcoholicity: 15, net: 120, }, shochu: { name: '焼酎', glass: '焼酎グラス', unit: '杯', alcoholicity: 25, net: 60, }, whiskey: { name: 'ウイスキー', glass: 'ショットグラス', unit: '杯', alcoholicity: 40, net: 40, }, }
Instance Method Summary collapse
- #drink(kind, glasses = 1) ⇒ Object
- #dump ⇒ Object
- #hangover? ⇒ Boolean
-
#initialize(drunker_weight, drinking_hours) ⇒ AntiHangover
constructor
A new instance of AntiHangover.
- #permissible_alcohol ⇒ Object
- #total_drunk ⇒ Object
Constructor Details
#initialize(drunker_weight, drinking_hours) ⇒ AntiHangover
Returns a new instance of AntiHangover.
52 53 54 55 56 57 58 |
# File 'lib/pebbles/anti_hangover.rb', line 52 def initialize drunker_weight, drinking_hours @drunker_weight = drunker_weight @drinking_hours = drinking_hours raise ArgumentError unless @drunker_weight > 0 raise ArgumentError unless @drinking_hours > 0 @drunk_glasses = Hash[ALCOHOL_TYPE.keys.zip(Array.new(ALCOHOL_TYPE.count).fill(0))] end |
Instance Method Details
#drink(kind, glasses = 1) ⇒ Object
64 65 66 67 |
# File 'lib/pebbles/anti_hangover.rb', line 64 def drink kind, glasses = 1 raise ArgumentError unless ALCOHOL_TYPE.include?(kind) @drunk_glasses[kind] += glasses end |
#dump ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pebbles/anti_hangover.rb', line 80 def dump result = "体重#{@drunker_weight}kgのあなたは、\n" drunk_logs = '' @drunk_glasses.each do |kind, glasses| drunk_logs += "、\n" unless drunk_logs.empty? glass = ALCOHOL_TYPE[kind] drunk_logs += "#{glass[:name]}を#{glass[:glass]}に#{glasses}#{glass[:unit]}" end percentage = (total_drunk.to_f / permissible_alcohol.to_f * 1000.0).floor / 10.0 result += drunk_logs + "飲んで、\n#{@drinking_hours}時間後に起きなければなりません。\n\n" result += 'そのときあなたは、おそらく二日酔いに' result += "#{hangover? ? 'なる' : 'ならない'}でしょう。\n\n" result += "(許容量の#{percentage}%を飲んでいます)\n" result end |
#hangover? ⇒ Boolean
76 77 78 |
# File 'lib/pebbles/anti_hangover.rb', line 76 def hangover? total_drunk > permissible_alcohol end |
#permissible_alcohol ⇒ Object
60 61 62 |
# File 'lib/pebbles/anti_hangover.rb', line 60 def permissible_alcohol (15 * @drunker_weight * @drinking_hours / 80).round end |
#total_drunk ⇒ Object
69 70 71 72 73 74 |
# File 'lib/pebbles/anti_hangover.rb', line 69 def total_drunk @drunk_glasses.inject(0) do |total, drunk| glass = ALCOHOL_TYPE[drunk.first] total + (drunk.last * glass[:alcoholicity] * glass[:net] / 100.0).round end end |