aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-03-02 23:02:44 +0100
committerXavier Noria <fxn@hashref.com>2010-03-02 23:02:44 +0100
commit8627281e3b75dbafc0adee7ab4f3d28a177aa9e5 (patch)
treeccfb24c9e95109b56662a836c046e305ae9643e1 /railties/guides
parent3084898ca6d98d37da64705d755e189e36040339 (diff)
downloadrails-8627281e3b75dbafc0adee7ab4f3d28a177aa9e5.tar.gz
rails-8627281e3b75dbafc0adee7ab4f3d28a177aa9e5.tar.bz2
rails-8627281e3b75dbafc0adee7ab4f3d28a177aa9e5.zip
AS guide: documents String#constantize
Diffstat (limited to 'railties/guides')
-rw-r--r--railties/guides/source/active_support_core_extensions.textile43
1 files changed, 43 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index 26e4113edf..149ebefbab 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -1484,6 +1484,49 @@ Note that +classify+ returns a class name as a string. You can get the actual cl
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+h5. +constantize+
+
+The method +constantize+ resolves the constant reference expression in its receiver:
+
+<ruby>
+"Fixnum".constantize # => Fixnum
+
+module M
+ X = 1
+end
+"M::X".constantize # => 1
+</ruby>
+
+If the string evaluates to no known constant, or its content is not even a valid constant name, +constantize+ raises +NameError+.
+
+Constant name resolution by +constantize+ starts always at the top-level +Object+ even if there is no leading "::".
+
+<ruby>
+X = :in_Object
+module M
+ X = :in_M
+
+ X # => :in_M
+ "::X".constantize # => :in_Object
+ "X".constantize # => :in_Object (!)
+end
+</ruby>
+
+So, it is in general not equivalent to what Ruby would do in the same spot, had a real constant be evaluated.
+
+Mailer test cases obtain the mailer being tested from the name of the test class using +constantize+:
+
+<ruby>
+# action_mailer/test_case.rb
+def determine_default_mailer(name)
+ name.sub(/Test$/, '').constantize
+rescue NameError => e
+ raise NonInferrableMailerError.new(name)
+end
+</ruby>
+
+NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+
h3. Extensions to +Numeric+
h4. Bytes