aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-08-06 17:31:57 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2008-08-06 17:31:57 -0700
commited8a882e47e07b470b71cacd8cd50e251dca4d27 (patch)
tree0f235c30d417d48189658148760bf3ab0ad43bd0 /activesupport/lib/active_support/inflector.rb
parentf5bcbde1e387020c7f4968515921a3ccee3dcda2 (diff)
downloadrails-ed8a882e47e07b470b71cacd8cd50e251dca4d27.tar.gz
rails-ed8a882e47e07b470b71cacd8cd50e251dca4d27.tar.bz2
rails-ed8a882e47e07b470b71cacd8cd50e251dca4d27.zip
JRuby: improve constantize performance. [#410 state:resolved]
Diffstat (limited to 'activesupport/lib/active_support/inflector.rb')
-rw-r--r--activesupport/lib/active_support/inflector.rb13
1 files changed, 8 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index 6651569d33..c2738b39fc 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -291,11 +291,14 @@ module ActiveSupport
# NameError is raised when the name is not in CamelCase or the constant is
# unknown.
def constantize(camel_cased_word)
- unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
- raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
- end
+ names = camel_cased_word.split('::')
+ names.shift if names.empty? || names.first.empty?
- Object.module_eval("::#{$1}", __FILE__, __LINE__)
+ constant = Object
+ names.each do |name|
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ end
+ constant
end
# Turns a number into an ordinal string used to denote the position in an
@@ -326,4 +329,4 @@ require 'active_support/inflections'
require 'active_support/core_ext/string/inflections'
unless String.included_modules.include?(ActiveSupport::CoreExtensions::String::Inflections)
String.send :include, ActiveSupport::CoreExtensions::String::Inflections
-end \ No newline at end of file
+end