開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 14章(ブロックとプロック), 14.4(練習問題)、おじいさんの時計 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
おじいさんの時計
コード(TextWrangler)
sample.rb
#!/usr/bin/env ruby1.9 # -*- coding: utf-8 -*- def groundfather_clock &block h = Time.new.hour h = 12 if h == 0 h -= 12 if h > 13 h.times do block.call end end puts Time.new groundfather_clock do puts 'DONG!' end
入出力結果(Terminal)
$ ./sample.rb 2012-09-17 16:02:45 +0900 DONG! DONG! DONG! DONG! $
0 コメント:
コメントを投稿