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 262fef7ed57520b857605a0105fe7ba9265654f6.
jeremy (author)
Mon Dec 15 18:20:18 -0800 2008
commit  19be3d35b38b6685789d8d343617d465a3652717
tree    a9556ab5a6c6c4972084287757be2cfc905dbfe7
parent  0d48408dccd2b69754310cb3702ddfe705dbcd12
...
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
...
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
0
@@ -330,30 +330,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
...
180
181
182
183
184
185
 
186
187
188
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
5
6
 
7
8
9
...
167
168
169
 
 
 
170
171
172
173
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
@@ -180,9 +167,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