Permalink
Browse files

Fix constantize so top level constants are looked up properly.

  • Loading branch information...
1 parent 32b917b commit c2239a3e385215751ae4660217b93ac961d801f5 @marcandre committed May 5, 2012
@@ -214,7 +214,7 @@ def constantize(camel_cased_word) #:nodoc:
names.inject(Object) do |constant, name|
candidate = constant.const_get(name)
- if constant.const_defined?(name, false) || !Object.const_defined?(name)
+ if constant.const_defined?(name, false) || constant == Object || !Object.const_defined?(name)
candidate
else
# Go down the ancestors to check it it's owned
@@ -12,15 +12,29 @@ class Gas
end
end
+class Object
+ module AddtlGlobalConstants
+ class Case
+ class Dice
+ end
+ end
+ end
+ include AddtlGlobalConstants
+end
+
module ConstantizeTestCases
def run_constantize_tests_on
assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") }
assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") }
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
+ assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
+ assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal Object, yield("") }
+ assert_nothing_raised { assert_equal Object, yield("::") }
assert_raise(NameError) { yield("UnknownClass") }
assert_raise(NameError) { yield("UnknownClass::Ace") }
assert_raise(NameError) { yield("UnknownClass::Ace::Base") }
@@ -38,8 +52,12 @@ def run_safe_constantize_tests_on
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
+ assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
+ assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal Object, yield("") }
+ assert_nothing_raised { assert_equal Object, yield("::") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass::Ace") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass::Ace::Base") }

0 comments on commit c2239a3

Please sign in to comment.