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  ed8a882e47e07b470b71cacd8cd50e251dca4d27
tree    0f235c30d417d48189658148760bf3ab0ad43bd0
parent  f5bcbde1e387020c7f4968515921a3ccee3dcda2
...
291
292
293
294
295
296
 
 
297
298
 
 
 
 
 
299
300
301
...
326
327
328
329
330
 
...
291
292
293
 
 
 
294
295
296
 
297
298
299
300
301
302
303
304
...
329
330
331
 
332
333
0
@@ -291,11 +291,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
0
@@ -326,4 +329,4 @@ require 'active_support/inflections'
0
 require 'active_support/core_ext/string/inflections'
0
 unless String.included_modules.include?(ActiveSupport::CoreExtensions::String::Inflections)
0
   String.send :include, ActiveSupport::CoreExtensions::String::Inflections
0
-end
0
\ No newline at end of file
0
+end

Comments