Skip to content
Browse files

AS guide: documents duplicable?

  • Loading branch information...
1 parent 5c0a76c commit 9b43e6b54e41d10a09f96e6ace338326b80e1508 @fxn fxn committed Jul 11, 2009
Showing with 34 additions and 0 deletions.
  1. +34 −0 railties/guides/source/active_support_overview.textile
View
34 railties/guides/source/active_support_overview.textile
@@ -51,6 +51,40 @@ The method +present?+ is equivalent to +!blank?+:
assert @response.body.present? # same as !@response.body.blank?
</ruby>
+h4. +duplicable?+
+
+A few fundamental objects in Ruby are singletons. For example, in the whole live of a program the integer 1 refers always to the same instance:
+
+<ruby>
+1.object_id # => 3
+Math.cos(0).to_i.object_id # => 3
+</ruby>
+
+Hence, there's no way these objects can be duplicated through +dup+ or +clone+:
+
+<ruby>
+true.dup # => TypeError: can't dup TrueClass
+</ruby>
+
+Some numbers which are not singletons are not duplicable either:
+
+<ruby>
+0.0.clone # => allocator undefined for Float
+(2**1024).clone # => allocator undefined for Bignum
+</ruby>
+
+Active Support provides +duplicable?+ to programmatically query an object about this property:
+
+<ruby>
+"".duplicable? # => true
+false.duplicable? # => false
+</ruby>
+
+By definition all objects are +duplicable?+ except +nil+, +false+, +true+, symbols, and numbers.
+
+WARNING. Using +duplicable?+ is discouraged because it depends on a hard-coded list. Classes have means to disallow duplication like removing +dup+ and +clone+ or raising exceptions from them, only +rescue+ can tell.
+
+
h4. +metaclass+
The method +metaclass+ returns the singleton class on any object:

0 comments on commit 9b43e6b

Please sign in to comment.
Something went wrong with that request. Please try again.