生年月日から年齢を求める
Rubyには生年月日から年齢を求めるメソッドは存在しない。このため生年月日から年齢を求めるには、Date
クラスを使って自分で計算をする必要がある。
require 'date' birthday = Date.new(1986, 12, 1) today = Date.today # 2014/3/27 age = today.year - birthday.year if today.month < birthday.month or (today.month == birthday.month and today.day < birthday.day) age -= 1 # まだ誕生日を迎えていない end p age #=> 27