Class: Age

Inherits:
Object
  • Object
show all
Defined in:
lib/age.rb,
lib/age/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Age

Returns a new instance of Age.



7
8
9
10
# File 'lib/age.rb', line 7

def initialize(options)
  @birth = options[:birth] || DateTime.now.offset(0)
  @end = options[:end] || DateTime.now.offset(0)
end

Instance Attribute Details

#birthObject

Returns the value of attribute birth.



5
6
7
# File 'lib/age.rb', line 5

def birth
  @birth
end

#endObject

Returns the value of attribute end.



5
6
7
# File 'lib/age.rb', line 5

def end
  @end
end

Instance Method Details

#inspectObject



27
28
29
# File 'lib/age.rb', line 27

def inspect
  years.inspect
end

#to_iObject



23
24
25
# File 'lib/age.rb', line 23

def to_i
  years
end

#yearsObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/age.rb', line 12

def years
  leep_year = 2004
  end_with_leep_year = DateTime.new(leep_year, @end.month, @end.day, @end.hour, @end.min, @end.sec)
  birth_with_leep_year = DateTime.new(leep_year, @birth.month, @birth.day, @birth.hour, @birth.min, @birth.sec)
  if end_with_leep_year < birth_with_leep_year
    @end.year - @birth.year - 1
  else
    @end.year - @birth.year
  end
end