RubyでHQ9+
書きかけで放置していたのを完成させました
class HQ9Plus def initialize(source) @source = source @count = 0 end def run @source.each_char do |c| case c when "H" hello when "Q" source when "9" bottles when "+" count end end end private def hello puts "Hello World" end def source puts @source end def count @count += 1 end def bottles 99.downto(0) do |i| beforeBottles = "#{i} bottle" afterBottles = "#{i-1} bottle" action = "Take one down and pass it around" if i == 2 beforeBottles += "s" elsif i == 0 afterBottles = "99 bottles" action = "Go to the store and buy some more" elsif i != 1 beforeBottles += "s" afterBottles += "s" else afterBottles = "no more bottles" end puts "#{beforeBottles.capitalize} of beer on the wall, #{beforeBottles} of beer on the wall." puts "#{action}, #{afterBottles} of beer on the wall." puts "" end end end HQ9Plus.new(ARGV[0]).run
はまったところ:
elsif
を
else if
と書き、
syntax error, unexpected $end, expecting keyword_end
が出続けたこと
どうせこの構文こうやって書くんだろーとか思ってはいけないガクガク