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

[#410 state:open]

This reverts commit 87790e00ec4a0b24146de3757d1d6892689b05e4.
jeremy (author)
Mon Dec 15 18:19:56 -0800 2008
commit  d7b7ff0556ea5a66aeda455141ba06b558671e3d
tree    8d9e51b51e894ea4c574eacdc9f81e9920fdb8fc
parent  75f55960eb419d35966ddd32e2bc87bb0b6a1f0e
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
351
352
...
323
324
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
327
328
329
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
0
@@ -323,30 +323,47 @@ module ActiveSupport
0
       underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
     end
0
 
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 { |name| constant = constant.const_get(name) }
0
-      constant
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
     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
10
11
12
13
14
15
16
17
18
19
20
21
22
...
174
175
176
177
178
179
 
180
181
182
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
5
6
 
7
8
9
...
161
162
163
 
 
 
164
165
166
167
0
@@ -2,21 +2,8 @@ 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
@@ -174,9 +161,7 @@ class InflectorTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_constantize_does_lexical_lookup
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
+    assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
0
   end
0
 
0
   def test_ordinal

Comments