Class: WhenEaster::EasterCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/when_easter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = nil) ⇒ EasterCalendar

Returns a new instance of EasterCalendar.



5
6
7
8
9
10
11
12
# File 'lib/when_easter.rb', line 5

def initialize(year = nil)
  @year = Time.now.year if year.nil?
  @year = year if year.is_a?(Integer)
  @year = year.to_i if year.is_a?(String)
  @roman_easter = self.class.find_roman_easter_date(@year)
  @greek_easter = self.class.find_greek_easter_date(@year)
  @distance_in_weeks = ((@greek_easter-@roman_easter)/3600/24/7).to_i
end

Instance Attribute Details

#distance_in_weeksObject (readonly)

Returns the value of attribute distance_in_weeks.



4
5
6
# File 'lib/when_easter.rb', line 4

def distance_in_weeks
  @distance_in_weeks
end

#greek_easterObject (readonly)

Returns the value of attribute greek_easter.



4
5
6
# File 'lib/when_easter.rb', line 4

def greek_easter
  @greek_easter
end

#roman_easterObject (readonly)

Returns the value of attribute roman_easter.



4
5
6
# File 'lib/when_easter.rb', line 4

def roman_easter
  @roman_easter
end

#yearObject (readonly)

Returns the value of attribute year.



4
5
6
# File 'lib/when_easter.rb', line 4

def year
  @year
end

Class Method Details

.find_greek_easter_date(year) ⇒ Object

> Sun Apr 24 00:00:00 +0200 2011



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/when_easter.rb', line 56

def self.find_greek_easter_date(year)
  a = year % 19
  b = year % 4
  c = year % 7
  d = (19 * a + 16) % 30
  e = (2 * b + 4 * c + 6 * d) % 7;
  easter = 3 + d + e;
  if easter <= 30
    Time.local(year, 4, easter)
  else
    Time.local(year, 5, (easter - 30))
  end
end

.find_roman_easter_date(year) ⇒ Object

> Sun Apr 24 00:00:00 +0200 2011



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/when_easter.rb', line 27

def self.find_roman_easter_date(year)
  g = year % 19 +1
  s = (year -1600) / 100 - (year-1600) / 400
  l = (((year - 1400) / 100) * 8) / 25

  p_2 = (3-11*g +s -l) % 30
  if p_2 == 29 || (p_2 == 28 && g > 11) 
    p = p_2 -1
  else
    p= p_2
  end

  d= (year + year / 4 - year / 100 + year / 400) % 7
  d_2 = (8-d) % 7

  p_3 = (80 + p) % 7
  x_2 = d_2 - p_3

  x = (x_2 -1) % 7 +1
  e = p+x

  if e < 11 
    Time.local(year,3,e+21)
  else
    Time.local(year,4,e-10)
  end
end

Instance Method Details

#findObject



13
14
15
# File 'lib/when_easter.rb', line 13

def find
  find_roman+"\n"+find_greek+"\n"+find_distance
end

#find_distanceObject



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

def find_distance
  "In the year #{@year} the distance between Roman and Greek Easter is #{distance_in_weeks} week(s)."
end

#find_greekObject



20
21
22
# File 'lib/when_easter.rb', line 20

def find_greek
  "In the year #{@year} the Greek Easter is on #{@greek_easter.strftime("%d %B")}."
end

#find_romanObject



17
18
19
# File 'lib/when_easter.rb', line 17

def find_roman
  "In the year #{@year} the Roman Easter is on #{@roman_easter.strftime("%d %B")}."
end