パスワードを忘れた? アカウント作成
651387 journal

tsykの日記: A genic program counter nobody can avoid. 2

日記 by tsyk
#!ruby
require 'date'

class Human
  attr_reader :date_of_birth
  def initialize(year, month, mday)
    @date_of_birth = Date.new(year, month, mday)
  end
  def age
    today = Date.today
    y = today.year - @date_of_birth.year
    m = today.month - @date_of_birth.month
    d = today.mday - @date_of_birth.mday
    y -= 1 unless m >= 0 and d >= 0
    y
  end
  def birthday?
    today = Date.today
    today.month == @date_of_birth.month and
      today.mday == @date_of_birth.mday
  end
end

tsyk = Human.new(19XX, 11, 26)

puts tsyk.date_of_birth
puts tsyk.age
puts "Happy Birthday!" if tsyk.birthday?
この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

日々是ハック也 -- あるハードコアバイナリアン

読み込み中...