提交 cc31c7fb 编写于 作者: J Jeremy Kemper

Merge pull request #16662 from robin850/duration-instance-of

Fix `ActiveSupport::Duration#instance_of?` to behave like `#is_a?`
* Add the `Duration#instance_of?` method that was previously delegated to the
internal `value` attribute.
* Fix the `ActiveSupport::Duration#instance_of?` method to return the right
value with the class itself since it was previously delegated to the
internal value.
*Robin Dupret*
......
......@@ -35,12 +35,12 @@ def -@ #:nodoc:
end
def is_a?(klass) #:nodoc:
instance_of?(klass) || value.is_a?(klass)
Duration == klass || value.is_a?(klass)
end
alias :kind_of? :is_a?
def instance_of?(klass) # :nodoc:
Duration == klass
Duration == klass || value.instance_of?(klass)
end
# Returns +true+ if +other+ is also a Duration instance with the
......
......@@ -20,6 +20,12 @@ class << k; undef_method :== end
assert !d.is_a?(k)
end
def test_instance_of
assert 1.minute.instance_of?(Fixnum)
assert 2.days.instance_of?(ActiveSupport::Duration)
assert !3.second.instance_of?(Numeric)
end
def test_threequals
assert ActiveSupport::Duration === 1.day
assert !(ActiveSupport::Duration === 1.day.to_i)
......@@ -35,17 +41,15 @@ def test_equals
end
def test_eql
rubinius_skip "Rubinius' #eql? definition relies on #instance_of? " \
"which behaves oddly for the sake of backward-compatibility."
assert 1.minute.eql?(1.minute)
assert 2.days.eql?(48.hours)
assert !1.second.eql?(1)
assert !1.eql?(1.second)
end
def test_instance_of
assert !1.minute.instance_of?(Fixnum)
assert !2.days.instance_of?(Fixnum)
end
def test_inspect
assert_equal '0 seconds', 0.seconds.inspect
assert_equal '1 month', 1.month.inspect
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册