提交 2cb85581 编写于 作者: Y yuuji.yaginuma

change return value of `duplicable?` with Ruby 2.4+

`NilClass`, `FalseClass`, `TrueClass`, `Symbol` and `Numeric` can dup
with Ruby 2.4+.

Ref: https://bugs.ruby-lang.org/issues/12979
上级 1373f9bd
* Change return value of `NilClass#duplicable?`, `FalseClass#duplicable?`,
`TrueClass#duplicable?`, `Symbol#duplicable?` and `Numeric#duplicable?`
to true with Ruby 2.4+. These classes can dup with Ruby 2.4+.
*Yuji Yaginuma*
* Remove deprecated class `ActiveSupport::Concurrency::Latch` * Remove deprecated class `ActiveSupport::Concurrency::Latch`
*Andrew White* *Andrew White*
......
#-- #--
# Most objects are cloneable, but not all. For example you can't dup +nil+: # Most objects are cloneable, but not all. For example you can't dup methods:
# #
# nil.dup # => TypeError: can't dup NilClass # method(:puts).dup # => TypeError: allocator undefined for Method
# #
# Classes may signal their instances are not duplicable removing +dup+/+clone+ # Classes may signal their instances are not duplicable removing +dup+/+clone+
# or raising exceptions from them. So, to dup an arbitrary object you normally # or raising exceptions from them. So, to dup an arbitrary object you normally
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
class Object class Object
# Can you safely dup this object? # Can you safely dup this object?
# #
# False for +nil+, +false+, +true+, symbol, number, method objects; # False for method objects;
# true otherwise. # true otherwise.
def duplicable? def duplicable?
true true
...@@ -27,6 +27,10 @@ def duplicable? ...@@ -27,6 +27,10 @@ def duplicable?
end end
class NilClass class NilClass
begin
nil.dup
rescue TypeError
# +nil+ is not duplicable: # +nil+ is not duplicable:
# #
# nil.duplicable? # => false # nil.duplicable? # => false
...@@ -34,9 +38,14 @@ class NilClass ...@@ -34,9 +38,14 @@ class NilClass
def duplicable? def duplicable?
false false
end end
end
end end
class FalseClass class FalseClass
begin
false.dup
rescue TypeError
# +false+ is not duplicable: # +false+ is not duplicable:
# #
# false.duplicable? # => false # false.duplicable? # => false
...@@ -44,9 +53,14 @@ class FalseClass ...@@ -44,9 +53,14 @@ class FalseClass
def duplicable? def duplicable?
false false
end end
end
end end
class TrueClass class TrueClass
begin
true.dup
rescue TypeError
# +true+ is not duplicable: # +true+ is not duplicable:
# #
# true.duplicable? # => false # true.duplicable? # => false
...@@ -54,9 +68,14 @@ class TrueClass ...@@ -54,9 +68,14 @@ class TrueClass
def duplicable? def duplicable?
false false
end end
end
end end
class Symbol class Symbol
begin
:symbol.dup
rescue TypeError
# Symbols are not duplicable: # Symbols are not duplicable:
# #
# :my_symbol.duplicable? # => false # :my_symbol.duplicable? # => false
...@@ -64,9 +83,14 @@ class Symbol ...@@ -64,9 +83,14 @@ class Symbol
def duplicable? def duplicable?
false false
end end
end
end end
class Numeric class Numeric
begin
1.dup
rescue TypeError
# Numbers are not duplicable: # Numbers are not duplicable:
# #
# 3.duplicable? # => false # 3.duplicable? # => false
...@@ -74,6 +98,7 @@ class Numeric ...@@ -74,6 +98,7 @@ class Numeric
def duplicable? def duplicable?
false false
end end
end
end end
require "bigdecimal" require "bigdecimal"
......
...@@ -4,8 +4,13 @@ ...@@ -4,8 +4,13 @@
require "active_support/core_ext/numeric/time" require "active_support/core_ext/numeric/time"
class DuplicableTest < ActiveSupport::TestCase class DuplicableTest < ActiveSupport::TestCase
if RUBY_VERSION >= "2.4.0"
RAISE_DUP = [method(:puts)] RAISE_DUP = [method(:puts)]
ALLOW_DUP = ["1", Object.new, /foo/, [], {}, Time.now, Class.new, Module.new, BigDecimal.new("4.56"), nil, false, true, :symbol, 1, 2.3]
else
RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, method(:puts)]
ALLOW_DUP = ["1", Object.new, /foo/, [], {}, Time.now, Class.new, Module.new, BigDecimal.new("4.56")] ALLOW_DUP = ["1", Object.new, /foo/, [], {}, Time.now, Class.new, Module.new, BigDecimal.new("4.56")]
end
def test_duplicable def test_duplicable
rubinius_skip "* Method#dup is allowed at the moment on Rubinius\n" \ rubinius_skip "* Method#dup is allowed at the moment on Rubinius\n" \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册