aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2009-07-11 01:05:22 +0200
committerXavier Noria <fxn@hashref.com>2009-07-11 01:05:22 +0200
commit9b43e6b54e41d10a09f96e6ace338326b80e1508 (patch)
treed4d8062fd72f2eb69046f6033305e5c05b91a89f /railties/guides/source
parent5c0a76c41a9f29ea635c79cdc966d2a057e1b107 (diff)
downloadrails-9b43e6b54e41d10a09f96e6ace338326b80e1508.tar.gz
rails-9b43e6b54e41d10a09f96e6ace338326b80e1508.tar.bz2
rails-9b43e6b54e41d10a09f96e6ace338326b80e1508.zip
AS guide: documents duplicable?
Diffstat (limited to 'railties/guides/source')
-rw-r--r--railties/guides/source/active_support_overview.textile34
1 files changed, 34 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile
index 715e5c17d8..30a593a0d7 100644
--- a/railties/guides/source/active_support_overview.textile
+++ b/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: