Navigation Menu

Skip to content

Commit

Permalink
Fix Resque::Helpers#constantize to work correctly on 1.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlong authored and defunkt committed Oct 10, 2011
1 parent c8a3e0a commit aca9e2f
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/resque/helpers.rb
Expand Up @@ -40,9 +40,23 @@ def classify(dashed_word)
dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
end

# Given a camel cased word, returns the constant it represents
# Tries to find a constant with the name specified in the argument string:
#
# constantize('JobName') # => JobName
# constantize("Module") # => Module
# constantize("Test::Unit") # => Test::Unit
#
# The name is assumed to be the one of a top-level constant, no matter
# whether it starts with "::" or not. No lexical context is taken into
# account:
#
# C = 'outside'
# module M
# C = 'inside'
# C # => 'inside'
# constantize("C") # => 'outside', same as ::C
# end
#
# NameError is raised when the constant is unknown.
def constantize(camel_cased_word)
camel_cased_word = camel_cased_word.to_s

Expand All @@ -55,7 +69,13 @@ def constantize(camel_cased_word)

constant = Object
names.each do |name|
constant = constant.const_get(name) || constant.const_missing(name)
args = Module.method(:const_get).arity != 1 ? [false] : []

if constant.const_defined?(name, *args)
constant = constant.const_get(name)
else
constant = constant.const_missing(name)
end
end
constant
end
Expand Down

0 comments on commit aca9e2f

Please sign in to comment.