Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

JRuby: improve constantize performance. [#410 state:resolved]

  • Loading branch information...
commit ed8a882e47e07b470b71cacd8cd50e251dca4d27 1 parent f5bcbde
@jeremy jeremy authored
Showing with 8 additions and 5 deletions.
  1. +8 −5 activesupport/lib/active_support/inflector.rb
View
13 activesupport/lib/active_support/inflector.rb
@@ -291,11 +291,14 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
# 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 @@ def ordinalize(number)
require 'active_support/core_ext/string/inflections'
unless String.included_modules.include?(ActiveSupport::CoreExtensions::String::Inflections)
String.send :include, ActiveSupport::CoreExtensions::String::Inflections
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.