You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.1 KiB
131 lines
3.1 KiB
diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb
|
|
index 9bc5ee6..d4bf8ce 100644
|
|
--- a/activesupport/lib/active_support/core_ext/object/duplicable.rb
|
|
+++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb
|
|
@@ -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,52 +27,77 @@ def duplicable?
|
|
end
|
|
|
|
class NilClass
|
|
- # +nil+ is not duplicable:
|
|
- #
|
|
- # nil.duplicable? # => false
|
|
- # nil.dup # => TypeError: can't dup NilClass
|
|
- def duplicable?
|
|
- false
|
|
+ begin
|
|
+ nil.dup
|
|
+ rescue TypeError
|
|
+
|
|
+ # +nil+ is not duplicable:
|
|
+ #
|
|
+ # nil.duplicable? # => false
|
|
+ # nil.dup # => TypeError: can't dup NilClass
|
|
+ def duplicable?
|
|
+ false
|
|
+ end
|
|
end
|
|
end
|
|
|
|
class FalseClass
|
|
- # +false+ is not duplicable:
|
|
- #
|
|
- # false.duplicable? # => false
|
|
- # false.dup # => TypeError: can't dup FalseClass
|
|
- def duplicable?
|
|
- false
|
|
+ begin
|
|
+ false.dup
|
|
+ rescue TypeError
|
|
+
|
|
+ # +false+ is not duplicable:
|
|
+ #
|
|
+ # false.duplicable? # => false
|
|
+ # false.dup # => TypeError: can't dup FalseClass
|
|
+ def duplicable?
|
|
+ false
|
|
+ end
|
|
end
|
|
end
|
|
|
|
class TrueClass
|
|
- # +true+ is not duplicable:
|
|
- #
|
|
- # true.duplicable? # => false
|
|
- # true.dup # => TypeError: can't dup TrueClass
|
|
- def duplicable?
|
|
- false
|
|
+ begin
|
|
+ true.dup
|
|
+ rescue TypeError
|
|
+
|
|
+ # +true+ is not duplicable:
|
|
+ #
|
|
+ # true.duplicable? # => false
|
|
+ # true.dup # => TypeError: can't dup TrueClass
|
|
+ def duplicable?
|
|
+ false
|
|
+ end
|
|
end
|
|
end
|
|
|
|
class Symbol
|
|
- # Symbols are not duplicable:
|
|
- #
|
|
- # :my_symbol.duplicable? # => false
|
|
- # :my_symbol.dup # => TypeError: can't dup Symbol
|
|
- def duplicable?
|
|
- false
|
|
+ begin
|
|
+ :symbol.dup
|
|
+ rescue TypeError
|
|
+
|
|
+ # Symbols are not duplicable:
|
|
+ #
|
|
+ # :my_symbol.duplicable? # => false
|
|
+ # :my_symbol.dup # => TypeError: can't dup Symbol
|
|
+ def duplicable?
|
|
+ false
|
|
+ end
|
|
end
|
|
end
|
|
|
|
class Numeric
|
|
- # Numbers are not duplicable:
|
|
- #
|
|
- # 3.duplicable? # => false
|
|
- # 3.dup # => TypeError: can't dup Integer
|
|
- def duplicable?
|
|
- false
|
|
+ begin
|
|
+ 1.dup
|
|
+ rescue TypeError
|
|
+
|
|
+ # Numbers are not duplicable:
|
|
+ #
|
|
+ # 3.duplicable? # => false
|
|
+ # 3.dup # => TypeError: can't dup Integer
|
|
+ def duplicable?
|
|
+ false
|
|
+ end
|
|
end
|
|
end
|