aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_support_core_extensions.md
diff options
context:
space:
mode:
authorutilum <oz@utilum.com>2016-12-13 16:34:46 +0100
committerutilum <oz@utilum.com>2016-12-23 19:52:36 +0100
commit194f97c51e75d4d0e355091776af6e4c8bb83d82 (patch)
treebcf3fd3dd34f2cf0e2dd50939e6a73fb4c58774a /guides/source/active_support_core_extensions.md
parentcfdf6e13689c67a108b1f3980f59a4e3933369c3 (diff)
downloadrails-194f97c51e75d4d0e355091776af6e4c8bb83d82.tar.gz
rails-194f97c51e75d4d0e355091776af6e4c8bb83d82.tar.bz2
rails-194f97c51e75d4d0e355091776af6e4c8bb83d82.zip
Update list of `duplicable?` objects in AS core_ext guide [ci skip]
Complex and Rational objects can not be duplicated. Duplicable objects in Ruby 2.4.0 include: - most numbers - symbols - `nil` - `true` - `false`
Diffstat (limited to 'guides/source/active_support_core_extensions.md')
-rw-r--r--guides/source/active_support_core_extensions.md48
1 files changed, 31 insertions, 17 deletions
diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md
index 6bbc79a326..bfe6b7b7ea 100644
--- a/guides/source/active_support_core_extensions.md
+++ b/guides/source/active_support_core_extensions.md
@@ -135,36 +135,50 @@ NOTE: Defined in `active_support/core_ext/object/blank.rb`.
### `duplicable?`
-A few fundamental objects in Ruby are singletons. For example, in the whole life of a program the integer 1 refers always to the same instance:
+In Ruby 2.4 most objects can be duplicated throuh `dup` or `clone`, except
+method objects and certain numbers. Ruby 2.2 and 2.3 can't duplicate `nil`,
+`false`, `true`, and symbols, as well as instances of `Float`, `Fixnum`
+and `Bignum`.
```ruby
-1.object_id # => 3
-Math.cos(0).to_i.object_id # => 3
+"foo".dup # => "foo"
+"".dup # => ""
+1.method(:+).dup # => TypeError: allocator undefined for Method
+Complex(0).dup # => TypeError: can't copy Complex
```
-Hence, there's no way these objects can be duplicated through `dup` or `clone`:
+Active Support provides `duplicable?` to programmatically query an object about
+this property:
```ruby
-true.dup # => TypeError: can't dup TrueClass
+"foo".duplicable? # => true
+"".duplicable? # => true
+Rational(1).duplicable? # => false
+Complex(1).duplicable? # => false
+1.method(:+).duplicable? # => false
```
-Some numbers which are not singletons are not duplicable either:
+`duplicable?` returns the correct result for the version of Ruby used.
```ruby
-0.0.clone # => allocator undefined for Float
-(2**1024).clone # => allocator undefined for Bignum
-```
+# version 2.4
+nil.dup # => nil
+:my_symbol.dup # => :my_symbol
+1.dup # => 1
-Active Support provides `duplicable?` to programmatically query an object about this property:
+nil.duplicable? # => true
+:my_symbol.duplicable? # => true
+1.duplicable? # => true
-```ruby
-"foo".duplicable? # => true
-"".duplicable? # => true
-0.0.duplicable? # => false
-false.duplicable? # => false
-```
+# version 2.2 and 2.3
+nil.dup # => TypeError: can't dup NilClass
+:my_symbol.dup # => TypeError: can't dup Symbol
+1.dup # => TypeError: can't dup Fixnum
-By definition all objects are `duplicable?` except `nil`, `false`, `true`, symbols, numbers, class, module, and method objects.
+nil.duplicable? # => false
+:my_symbol.duplicable? # => false
+1.duplicable? # => false
+```
WARNING: Any class can disallow duplication by removing `dup` and `clone` or raising exceptions from them. Thus only `rescue` can tell whether a given arbitrary object is duplicable. `duplicable?` depends on the hard-coded list above, but it is much faster than `rescue`. Use it only if you know the hard-coded list is enough in your use case.