1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
yuuji.yaginuma 2016-12-07 09:24:50 +09:00
parent 1373f9bdcf
commit 2cb8558120
3 changed files with 71 additions and 35 deletions

View file

@ -1,3 +1,9 @@
* 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`
*Andrew White*

View file

@ -1,7 +1,7 @@
#--
# 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+
# or raising exceptions from them. So, to dup an arbitrary object you normally
@ -19,7 +19,7 @@
class Object
# Can you safely dup this object?
#
# False for +nil+, +false+, +true+, symbol, number, method objects;
# False for method objects;
# true otherwise.
def duplicable?
true
@ -27,6 +27,10 @@ class Object
end
class NilClass
begin
nil.dup
rescue TypeError
# +nil+ is not duplicable:
#
# nil.duplicable? # => false
@ -35,8 +39,13 @@ class NilClass
false
end
end
end
class FalseClass
begin
false.dup
rescue TypeError
# +false+ is not duplicable:
#
# false.duplicable? # => false
@ -45,8 +54,13 @@ class FalseClass
false
end
end
end
class TrueClass
begin
true.dup
rescue TypeError
# +true+ is not duplicable:
#
# true.duplicable? # => false
@ -55,8 +69,13 @@ class TrueClass
false
end
end
end
class Symbol
begin
:symbol.dup
rescue TypeError
# Symbols are not duplicable:
#
# :my_symbol.duplicable? # => false
@ -65,8 +84,13 @@ class Symbol
false
end
end
end
class Numeric
begin
1.dup
rescue TypeError
# Numbers are not duplicable:
#
# 3.duplicable? # => false
@ -75,6 +99,7 @@ class Numeric
false
end
end
end
require "bigdecimal"
class BigDecimal

View file

@ -4,8 +4,13 @@ require "active_support/core_ext/object/duplicable"
require "active_support/core_ext/numeric/time"
class DuplicableTest < ActiveSupport::TestCase
if RUBY_VERSION >= "2.4.0"
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")]
end
def test_duplicable
rubinius_skip "* Method#dup is allowed at the moment on Rubinius\n" \