2012年9月24日月曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)、この本の著者の年齢 を解いてみる。

その他参考書籍

この本の著者の年齢

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

puts "著者の年齢: #{102500 * (10 ** 4) / (365 * 24 * 60 * 60)}歳"

td = Time.new
birth = td - 102500 * (10 ** 4)
age = td.year - birth.year
age -= 1 if td.month < birth.month || 
  (td.month == birth.month && td.day < birth.day)

puts "Timeクラスで調べた場合の結果: #{age}歳"
puts "誕生日: #{birth}"

入出力結果(Terminal)

$ ./sample.rb
著者の年齢: 32歳
Timeクラスで調べた場合の結果: 32歳
誕生日: 1980-04-02 08:22:08 +0900
$

0 コメント:

コメントを投稿