public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make constantize look into ancestors

[#410 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
fcheung (author)
Sun Dec 14 02:07:06 -0800 2008
jeremy (committer)
Mon Dec 15 11:01:04 -0800 2008
commit  262fef7ed57520b857605a0105fe7ba9265654f6
tree    1cf1baaea3ded200027a99d7816a2d7d338cffe4
parent  7c18518105e98ccfd89fe64194ede27824dfe8b3
...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
375
376
...
330
331
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
0
@@ -330,47 +330,30 @@ module ActiveSupport
0
       underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
     end
0
 
0
-    # Ruby 1.9 introduces an inherit argument for Module#const_get and
0
-    # #const_defined? and changes their default behavior.
0
-    if Module.method(:const_get).arity == 1
0
-      # Tries to find a constant with the name specified in the argument string:
0
-      #
0
-      #   "Module".constantize     # => Module
0
-      #   "Test::Unit".constantize # => Test::Unit
0
-      #
0
-      # The name is assumed to be the one of a top-level constant, no matter whether
0
-      # it starts with "::" or not. No lexical context is taken into account:
0
-      #
0
-      #   C = 'outside'
0
-      #   module M
0
-      #     C = 'inside'
0
-      #     C               # => 'inside'
0
-      #     "C".constantize # => 'outside', same as ::C
0
-      #   end
0
-      #
0
-      # NameError is raised when the name is not in CamelCase or the constant is
0
-      # unknown.
0
-      def constantize(camel_cased_word)
0
-        names = camel_cased_word.split('::')
0
-        names.shift if names.empty? || names.first.empty?
0
-
0
-        constant = Object
0
-        names.each do |name|
0
-          constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
0
-        end
0
-        constant
0
-      end
0
-    else
0
-      def constantize(camel_cased_word) #:nodoc:
0
-        names = camel_cased_word.split('::')
0
-        names.shift if names.empty? || names.first.empty?
0
-
0
-        constant = Object
0
-        names.each do |name|
0
-          constant = constant.const_get(name, false) || constant.const_missing(name)
0
-        end
0
-        constant
0
-      end
0
+    # Tries to find a constant with the name specified in the argument string:
0
+    #
0
+    #   "Module".constantize     # => Module
0
+    #   "Test::Unit".constantize # => Test::Unit
0
+    #
0
+    # The name is assumed to be the one of a top-level constant, no matter whether
0
+    # it starts with "::" or not. No lexical context is taken into account:
0
+    #
0
+    #   C = 'outside'
0
+    #   module M
0
+    #     C = 'inside'
0
+    #     C               # => 'inside'
0
+    #     "C".constantize # => 'outside', same as ::C
0
+    #   end
0
+    #
0
+    # NameError is raised when the name is not in CamelCase or the constant is
0
+    # unknown.
0
+    def constantize(camel_cased_word)
0
+      names = camel_cased_word.split('::')
1
+      names.shift if names.empty? || names.first.empty?
0
+
0
+      constant = Object
0
+      names.each { |name| constant = constant.const_get(name) }
0
+      constant
0
     end
0
 
0
     # Turns a number into an ordinal string used to denote the position in an
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
5
6
 
7
8
9
...
167
168
169
170
 
 
 
171
172
173
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
180
181
182
 
183
184
185
186
187
188
0
@@ -2,8 +2,21 @@ require 'abstract_unit'
0
 require 'inflector_test_cases'
0
 
0
 module Ace
0
+  module Extension
0
+    def self.included(base)
0
+      base.extend(ClassMethods)
0
+    end
0
+
0
+    module ClassMethods
0
+      def mission_accomplished?
0
+        false
0
+      end
0
+    end
0
+  end
0
+
0
   module Base
0
     class Case
0
+      include Extension
0
     end
0
   end
0
 end
0
@@ -167,7 +180,9 @@ class InflectorTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_constantize_does_lexical_lookup
0
-    assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
0
+    assert_equal InflectorTest, ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest")
0
+    assert_nothing_raised { Ace::Base::Case::ClassMethods }
0
+    assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
0
   end
0
 
0
   def test_ordinal

Comments

Roman2K Mon Dec 15 14:30:38 -0800 2008 at activesupport/lib/active_support/inflector.rb L393

What’s the point of names.shift if names is empty?