aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-09-17 16:15:25 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-09-17 16:15:25 -0300
commitacda46d08181cb39d9adf56f5f983dc0ffaaf96f (patch)
treeddc361a9466d37f12ecb2392c97f806ec3a50585 /activesupport
parent84087966e84a9875fdf707193f376ffea8c80610 (diff)
parent369a107c8e30a15d03cb2e85242bc604fe7a02a3 (diff)
downloadrails-acda46d08181cb39d9adf56f5f983dc0ffaaf96f.tar.gz
rails-acda46d08181cb39d9adf56f5f983dc0ffaaf96f.tar.bz2
rails-acda46d08181cb39d9adf56f5f983dc0ffaaf96f.zip
Merge pull request #11794 from yoazt/duration-eql
Added method `#eql?` to `ActiveSupport::Duration`, in addition to `#==`. Conflicts: activesupport/CHANGELOG.md activesupport/lib/active_support/duration.rb activesupport/test/core_ext/duration_test.rb
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md15
-rw-r--r--activesupport/lib/active_support/duration.rb4
-rw-r--r--activesupport/test/core_ext/duration_test.rb4
3 files changed, 22 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index bc8d11a297..8b671a8c27 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*
+
* Time#change can now change nanoseconds (:nsec) as a higher-precision
alternative to microseconds (:usec).
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index a54494bc65..e861a17426 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -57,8 +57,10 @@ module ActiveSupport
@value.to_s
end
+ # Returns +true+ if +other+ is also a Duration instance, which has the
+ # same parts as this one.
def eql?(other)
- other.is_a?(Duration) && self == other
+ Duration === other && other.value.eql?(value)
end
def self.===(other) #:nodoc:
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 555bde6e04..881faf93f5 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -49,9 +49,13 @@ class DurationTest < ActiveSupport::TestCase
"which behaves oddly for the sake of backward-compatibility."
assert 1.minute.eql?(1.minute)
+ assert 1.minute.eql?(60.seconds)
assert 2.days.eql?(48.hours)
assert !1.second.eql?(1)
assert !1.eql?(1.second)
+ assert 1.minute.eql?(180.seconds - 2.minutes)
+ assert !1.minute.eql?(60)
+ assert !1.minute.eql?('foo')
end
def test_inspect