public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
JRuby: improve constantize performance.  [#410 state:resolved]
jeremy (author)
Wed Aug 06 17:31:57 -0700 2008
commit  88eec8327b179cd41314e85868879b46bcf2530e
tree    73c1e89a1968b0caae74e539260beb99eeda4415
parent  0498d32e5d5de1487612518a63870fdb07ff0081
...
277
278
279
280
281
282
 
 
283
284
 
 
 
 
 
285
286
287
...
277
278
279
 
 
 
280
281
282
 
283
284
285
286
287
288
289
290
0
@@ -277,11 +277,14 @@ module ActiveSupport
0
     # NameError is raised when the name is not in CamelCase or the constant is
0
     # unknown.
0
     def constantize(camel_cased_word)
0
-      unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
0
-        raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
0
-      end
0
+      names = camel_cased_word.split('::')
0
+      names.shift if names.empty? || names.first.empty?
0
 
0
-      Object.module_eval("::#{$1}", __FILE__, __LINE__)
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
 
0
     # Turns a number into an ordinal string used to denote the position in an

Comments