mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[ci skip] Rejigger the dublicable? wording a bit.
This commit is contained in:
parent
80ddb7ee2e
commit
f2f4fecdf4
1 changed files with 12 additions and 9 deletions
|
@ -135,10 +135,10 @@ NOTE: Defined in `active_support/core_ext/object/blank.rb`.
|
||||||
|
|
||||||
### `duplicable?`
|
### `duplicable?`
|
||||||
|
|
||||||
In Ruby 2.4 most objects can be duplicated throuh `dup` or `clone`, except
|
In Ruby 2.4 most objects can be duplicated via `dup` or `clone` except
|
||||||
method objects and certain numbers. Ruby 2.2 and 2.3 can't duplicate `nil`,
|
methods and certain numbers. Though Ruby 2.2 and 2.3 can't duplicate `nil`,
|
||||||
`false`, `true`, and symbols, as well as instances of `Float`, `Fixnum`
|
`false`, `true`, and symbols as well as instances `Float`, `Fixnum`,
|
||||||
and `Bignum`.
|
and `Bignum` instances.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
"foo".dup # => "foo"
|
"foo".dup # => "foo"
|
||||||
|
@ -147,8 +147,7 @@ and `Bignum`.
|
||||||
Complex(0).dup # => TypeError: can't copy Complex
|
Complex(0).dup # => TypeError: can't copy Complex
|
||||||
```
|
```
|
||||||
|
|
||||||
Active Support provides `duplicable?` to programmatically query an object about
|
Active Support provides `duplicable?` to query an object about this:
|
||||||
this property:
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
"foo".duplicable? # => true
|
"foo".duplicable? # => true
|
||||||
|
@ -158,10 +157,11 @@ Complex(1).duplicable? # => false
|
||||||
1.method(:+).duplicable? # => false
|
1.method(:+).duplicable? # => false
|
||||||
```
|
```
|
||||||
|
|
||||||
`duplicable?` returns the correct result for the version of Ruby used.
|
`duplicable?` matches Ruby's `dup` according to the Ruby version.
|
||||||
|
|
||||||
|
So in 2.4:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# version 2.4
|
|
||||||
nil.dup # => nil
|
nil.dup # => nil
|
||||||
:my_symbol.dup # => :my_symbol
|
:my_symbol.dup # => :my_symbol
|
||||||
1.dup # => 1
|
1.dup # => 1
|
||||||
|
@ -169,8 +169,11 @@ nil.dup # => nil
|
||||||
nil.duplicable? # => true
|
nil.duplicable? # => true
|
||||||
:my_symbol.duplicable? # => true
|
:my_symbol.duplicable? # => true
|
||||||
1.duplicable? # => true
|
1.duplicable? # => true
|
||||||
|
```
|
||||||
|
|
||||||
# version 2.2 and 2.3
|
Whereas in 2.2 and 2.3:
|
||||||
|
|
||||||
|
```ruby
|
||||||
nil.dup # => TypeError: can't dup NilClass
|
nil.dup # => TypeError: can't dup NilClass
|
||||||
:my_symbol.dup # => TypeError: can't dup Symbol
|
:my_symbol.dup # => TypeError: can't dup Symbol
|
||||||
1.dup # => TypeError: can't dup Fixnum
|
1.dup # => TypeError: can't dup Fixnum
|
||||||
|
|
Loading…
Reference in a new issue