lib/fat_period/period.rb in fat_period-1.1.1 vs lib/fat_period/period.rb in fat_period-1.2.0
- old
+ new
@@ -27,12 +27,13 @@
# @param last [Date, String] last date of Period
# @raise [ArgumentError] if string is not parseable as a Date or
# @raise [ArgumentError] if first date is later than last date
# @return [Period]
def initialize(first, last)
- @first = Date.ensure_date(first)
- @last = Date.ensure_date(last)
+ @first = Date.ensure_date(first).freeze
+ @last = Date.ensure_date(last).freeze
+ freeze
return unless @first > @last
raise ArgumentError, "Period's first date is later than its last date"
end
@@ -176,9 +177,23 @@
end
# Comparable does not include this.
def !=(other)
!(self == other)
+ end
+
+ # Return the hash value for this Period. Make Period's with identical
+ # values test eql? so that they may be used as hash keys.
+ #
+ # @return [Integer]
+ def hash
+ (first.hash | last.hash)
+ end
+
+ def eql?(other)
+ return nil unless other.is_a?(Period)
+
+ hash == other.hash
end
# Return whether this Period contains the given date.
#
# @param date [Date] date to test