File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -40,9 +40,23 @@ def classify(dashed_word)
40
40
dashed_word . split ( '-' ) . each { |part | part [ 0 ] = part [ 0 ] . chr . upcase } . join
41
41
end
42
42
43
- # Given a camel cased word, returns the constant it represents
43
+ # Tries to find a constant with the name specified in the argument string:
44
44
#
45
- # constantize('JobName') # => JobName
45
+ # constantize("Module") # => Module
46
+ # constantize("Test::Unit") # => Test::Unit
47
+ #
48
+ # The name is assumed to be the one of a top-level constant, no matter
49
+ # whether it starts with "::" or not. No lexical context is taken into
50
+ # account:
51
+ #
52
+ # C = 'outside'
53
+ # module M
54
+ # C = 'inside'
55
+ # C # => 'inside'
56
+ # constantize("C") # => 'outside', same as ::C
57
+ # end
58
+ #
59
+ # NameError is raised when the constant is unknown.
46
60
def constantize ( camel_cased_word )
47
61
camel_cased_word = camel_cased_word . to_s
48
62
@@ -55,7 +69,13 @@ def constantize(camel_cased_word)
55
69
56
70
constant = Object
57
71
names . each do |name |
58
- constant = constant . const_get ( name ) || constant . const_missing ( name )
72
+ args = Module . method ( :const_get ) . arity != 1 ? [ false ] : [ ]
73
+
74
+ if constant . const_defined? ( name , *args )
75
+ constant = constant . const_get ( name )
76
+ else
77
+ constant = constant . const_missing ( name )
78
+ end
59
79
end
60
80
constant
61
81
end
You can’t perform that action at this time.
0 commit comments