2012年9月17日月曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 14章(ブロックとプロック), 14.4(練習問題)、おじいさんの時計 を解いてみる。

その他参考書籍

おじいさんの時計

コード(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 コメント:

コメントを投稿