Class: EpubForge::Utils::Misc

Inherits:
Object
  • Object
show all
Defined in:
lib/epubforge/utils/misc.rb

Class Method Summary collapse

Class Method Details

.constantize(str) ⇒ Object

stolen from Rails constantize method, via StackOverflow stackoverflow.com/questions/3314475/how-do-i-get-class-object-from-string-abc-in-ruby



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/epubforge/utils/misc.rb', line 6

def self.constantize( str )
  names = str.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  
  constant
end