Skip to content

Instantly share code, notes, and snippets.

@ihower
Created September 11, 2011 03:23
Show Gist options
  • Save ihower/1209125 to your computer and use it in GitHub Desktop.
Save ihower/1209125 to your computer and use it in GitHub Desktop.
fix_resque_constantize.rb
module Resque
module Helpers
# Given a camel cased word, returns the constant it represents
#
# constantize('JobName') # => JobName
def constantize(camel_cased_word)
camel_cased_word = camel_cased_word.to_s
if camel_cased_word.include?('-')
camel_cased_word = classify(camel_cased_word)
end
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
# This's the original resque version:
# constant = constant.const_get(name) || constant.const_missing(name)
# And this is from the lastest ActiveSupport:
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment