diff options
author | Joost Lubach <joost@yoazt.com> | 2013-08-07 15:18:30 +0200 |
---|---|---|
committer | Joost Lubach <joost@yoazt.com> | 2013-08-07 16:46:30 +0200 |
commit | 369a107c8e30a15d03cb2e85242bc604fe7a02a3 (patch) | |
tree | 54542123900dfe48790f8043f474e3f8b4763eb4 /activesupport | |
parent | 4343e2dda25222d3586835bb25009e1a490cda8f (diff) | |
download | rails-369a107c8e30a15d03cb2e85242bc604fe7a02a3.tar.gz rails-369a107c8e30a15d03cb2e85242bc604fe7a02a3.tar.bz2 rails-369a107c8e30a15d03cb2e85242bc604fe7a02a3.zip |
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
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 15 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 8 |
3 files changed, 29 insertions, 0 deletions
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 |