Skip to content

Commit c4ef0ba

Browse files
author
Rick Hull
committed
trivial; comments and reorder
1 parent f088ecc commit c4ef0ba

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/compsci/date.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,10 @@ def self.month_name(number)
232232
# Date Instances
233233
#
234234

235+
# day count since epoch
235236
attr_reader :ordinal_day
236237

238+
# ordinal_day will be calculated if not passed in
237239
def initialize(year:, month:, day:, ordinal_day: nil)
238240
# validate year
239241
raise(InvalidYear, year) unless (MIN_Y..MAX_Y).cover?(year)
@@ -259,20 +261,19 @@ def initialize(year:, month:, day:, ordinal_day: nil)
259261
super(year:, month:, day:)
260262
end
261263

262-
def leap_year?
263-
Date.leap_year?(year)
264-
end
265-
266-
def <=>(other)
267-
@ordinal_day <=> other.ordinal_day
268-
end
269-
264+
# YYYY-MM-DD, per ISO 8601
270265
def to_s
271266
format('%04d-%02d-%02d', year, month, day)
272267
end
273268

274-
def name
275-
format("%s %d, %d", Date.month_name(month), day, year)
269+
# given a Date, return a count of days, possibly negative
270+
def diff(other)
271+
@ordinal_day - other.ordinal_day
272+
end
273+
274+
# per Comparable, return (-1, 0, +1) (less than, equals, greater than)
275+
def <=>(other)
276+
@ordinal_day <=> other.ordinal_day
276277
end
277278

278279
# given a count of days, return a new Date
@@ -281,13 +282,19 @@ def +(days)
281282
Date.from_ordinal(@ordinal_day + days)
282283
end
283284

285+
# "February 29, 2004"
286+
def name
287+
format("%s %d, %d", Date.month_name(month), day, year)
288+
end
289+
290+
# convenience
284291
def -(days)
285292
self.+(-1 * days)
286293
end
287294

288-
# given a Date, return a count of days, possibly negative
289-
def diff(other)
290-
@ordinal_day - other.ordinal_day
295+
# convenience
296+
def leap_year?
297+
Date.leap_year?(year)
291298
end
292299
end
293300
end

0 commit comments

Comments
 (0)