↑http://qiita.com/items/ebd8a56b41711ba459f9
こんな感じになった。
baseball_count.rb
class BsoCount
def initialize
@ball = @strike = @out = 0
end
def ball
@ball += 1
@ball = @strike = 0 if @ball == 4
end
def strike
@strike += 1
self.out if @strike == 3
end
def out
@out += 1
@out = 0 if @out == 3
@ball = @strike = 0
end
def hit
@ball = @strike = 0
end
def foulball
@strike += 1 unless @strike == 2
end
def result
[@out, @strike, @ball]
end
end
ansArr = []
bso = BsoCount.new
STDIN.gets.each_char do |actionChar|
case actionChar
when 's' then bso.strike
when 'b' then bso.ball
when 'f' then bso.foulball
when 'h' then bso.hit
when 'p' then bso.out
end
ansArr.push bso.result.to_s
end
print ansArr.join ','