From 369a107c8e30a15d03cb2e85242bc604fe7a02a3 Mon Sep 17 00:00:00 2001 From: Joost Lubach Date: Wed, 7 Aug 2013 15:18:30 +0200 Subject: Added method `#eql?` to `ActiveSupport::Duration`, in addition to `#==`. Currently, the following returns `false`, contrary to expectation: 1.minute.eql?(1.minute) Adding method `#eql?` will make this behave like expected. Method `#eql?` is just a bit stricter than `#==`, as it checks whether the argument is also a uration. Their parts may be different though. 1.minute.eql?(60.seconds) # => true 1.minute.eql?(60) # => false --- activesupport/CHANGELOG.md | 15 +++++++++++++++ activesupport/lib/active_support/duration.rb | 6 ++++++ activesupport/test/core_ext/duration_test.rb | 8 ++++++++ 3 files changed, 29 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index be97b744c8..267b87e5b7 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,18 @@ +* Added method `#eql?` to `ActiveSupport::Duration`, in addition to `#==`. + + Currently, the following returns `false`, contrary to expectation: + + 1.minute.eql?(1.minute) + + Adding method `#eql?` will make this behave like expected. Method `#eql?` is + just a bit stricter than `#==`, as it checks whether the argument is also a duration. Their + parts may be different though. + + 1.minute.eql?(60.seconds) # => true + 1.minute.eql?(60) # => false + + *Joost Lubach* + * Remove 'cow' => 'kine' irregular inflection from default inflections. *Andrew White* diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 2cb1f408b6..7cd27717f9 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -49,6 +49,12 @@ module ActiveSupport end end + # Returns +true+ if +other+ is also a Duration instance, which has the + # same parts as this one. + def eql?(other) + Duration === other && other.value.eql?(value) + end + def self.===(other) #:nodoc: other.is_a?(Duration) rescue ::NoMethodError diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 5e3987265b..53fce5d8f0 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -30,6 +30,14 @@ class DurationTest < ActiveSupport::TestCase assert !(1.day == 'foo') end + def test_eql + assert 1.minute.eql?(1.minute) + assert 1.minute.eql?(60.seconds) + assert 1.minute.eql?(180.seconds - 2.minutes) + assert !1.minute.eql?(60) + assert !1.minute.eql?('foo') + end + def test_inspect assert_equal '0 seconds', 0.seconds.inspect assert_equal '1 month', 1.month.inspect -- cgit v1.2.3