開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)この本の著者の年齢 を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
この本の著者の年齢
コード(TextWrangler)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding:utf-8 -*- td = Time.new birth = td - 10.25 * (10**8) age = td.year - birth.year age -= 1 if (td.month < birth.month) or (td.month == birth.month and td.day < birth.day) puts "今日の日付: #{td}" puts "著者の誕生日: #{birth}" puts "この本の著者の年齢: #{age}歳"
入出力結果(Terminal)
$ ./sample.rb 今日の日付: 2012-10-30 16:23:31 +0900 著者の誕生日: 1980-05-08 06:10:11 +0900 この本の著者の年齢: 32歳 $
ちなみにJavaScriptの場合。
コード(TextWrangler)
var td = new Date(); var birth = new Date(); birth.setSeconds(td.getSeconds() - 10.25 * Math.pow(10,8)); var age = td.getYear() - birth.getYear(); age = (td.getMonth() < birth.getMonth() || td.getMonth === birth.getMonth() && td.getDate() < birth.getDate()) ? age - 1 : age; var result = "今日の日付: " + td + "\n" + "著者の誕生日: " + birth + "\n" + "この本の著者の年齢: " + age + "歳\n"; $('#pre0').text(result);
0 コメント:
コメントを投稿