開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 10章(章全部で復習), 10.5(練習問題の続き)、「壁にビールが99本」を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
「壁にビールが99本」
コード(TextWrangler)
#!/usr/bin/env ruby #encoding: utf-8 def english_number num return "負ではない整数を指定してください" if num < 0 return 'zero' if num == 0 num_string = '' ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] others = [['hundred',2], ['thousand',3], ['million',6], ['billion',9], ['trillion',12]] left = num while others.length > 0 other = others.pop write = left / (10 ** other[1]) left = left - write * (10 ** other[1]) if write > 0 hundreds = english_number write num_string = num_string + hundreds + ' ' + other[0] if left > 0 num_string += ' ' end end end write = left / 10 left = left - write * 10 if write > 0 if(write == 1) and (left > 0) num_string += teenagers[left - 1] left = 0 else num_string += tens_place[left - 1] end if left > 0 num_string += '-' end end write = left left = 0 if write > 0 num_string += ones_place[write - 1] end num_string end n = 99 while 1 puts "#{english_number n} bottles of beer on the wall, #{english_number n} bottles of beer!" n -= 1 break if n == 1 puts "Take one down, pass it around, #{english_number n} bottles of beer on the wall!" end puts "Take one down, pass it around, #{english_number n} bottle of beer on the waall!" puts "#{english_number n} bottle of beer on the wall, #{english_number n} bottle of beer!" puts "Take one down, pass it around, no more bottles of beer on the wall!"
入出力結果(Terminal)
$ ruby sample.rb ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, ninety-nine bottles of beer on the wall! ninety-nine bottles of beer on the wall, ninety-nine bottles of beer! Take one down, pass it around, eighty-eight bottles of beer on the wall! eighty-eight bottles of beer on the wall, eighty-eight bottles of beer! Take one down, pass it around, seventy-seven bottles of beer on the wall! seventy-seven bottles of beer on the wall, seventy-seven bottles of beer! Take one down, pass it around, sixty-six bottles of beer on the wall! sixty-six bottles of beer on the wall, sixty-six bottles of beer! Take one down, pass it around, fifty-five bottles of beer on the wall! fifty-five bottles of beer on the wall, fifty-five bottles of beer! Take one down, pass it around, forty-four bottles of beer on the wall! forty-four bottles of beer on the wall, forty-four bottles of beer! Take one down, pass it around, thirty-three bottles of beer on the wall! thirty-three bottles of beer on the wall, thirty-three bottles of beer! Take one down, pass it around, twenty-two bottles of beer on the wall! twenty-two bottles of beer on the wall, twenty-two bottles of beer! Take one down, pass it around, ten-one bottles of beer on the wall! ten-one bottles of beer on the wall, ten-one bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, nineteen bottles of beer on the wall! nineteen bottles of beer on the wall, nineteen bottles of beer! Take one down, pass it around, eighteen bottles of beer on the wall! eighteen bottles of beer on the wall, eighteen bottles of beer! Take one down, pass it around, seventeen bottles of beer on the wall! seventeen bottles of beer on the wall, seventeen bottles of beer! Take one down, pass it around, sixteen bottles of beer on the wall! sixteen bottles of beer on the wall, sixteen bottles of beer! Take one down, pass it around, fifteen bottles of beer on the wall! fifteen bottles of beer on the wall, fifteen bottles of beer! Take one down, pass it around, fourteen bottles of beer on the wall! fourteen bottles of beer on the wall, fourteen bottles of beer! Take one down, pass it around, thirteen bottles of beer on the wall! thirteen bottles of beer on the wall, thirteen bottles of beer! Take one down, pass it around, twelve bottles of beer on the wall! twelve bottles of beer on the wall, twelve bottles of beer! Take one down, pass it around, eleven bottles of beer on the wall! eleven bottles of beer on the wall, eleven bottles of beer! Take one down, pass it around, ninety bottles of beer on the wall! ninety bottles of beer on the wall, ninety bottles of beer! Take one down, pass it around, nine bottles of beer on the wall! nine bottles of beer on the wall, nine bottles of beer! Take one down, pass it around, eight bottles of beer on the wall! eight bottles of beer on the wall, eight bottles of beer! Take one down, pass it around, seven bottles of beer on the wall! seven bottles of beer on the wall, seven bottles of beer! Take one down, pass it around, six bottles of beer on the wall! six bottles of beer on the wall, six bottles of beer! Take one down, pass it around, five bottles of beer on the wall! five bottles of beer on the wall, five bottles of beer! Take one down, pass it around, four bottles of beer on the wall! four bottles of beer on the wall, four bottles of beer! Take one down, pass it around, three bottles of beer on the wall! three bottles of beer on the wall, three bottles of beer! Take one down, pass it around, two bottles of beer on the wall! two bottles of beer on the wall, two bottles of beer! Take one down, pass it around, one bottle of beer on the waall! one bottle of beer on the wall, one bottle of beer! Take one down, pass it around, no more bottles of beer on the wall! $
今回の周ではirb(インタラクティブRuby)も少しずつ活用してみることに。
まだ上記の本を入手してないのでまた最初から。
本書を続けつつ、上記の本を早く入手してそっちに切り替え。本書も飽きてきたから早く入手しないと。。
Learning Rubyを入手したので今周が終わったら切り替え。
0 コメント:
コメントを投稿