Module: Kernel

Defined in:
lib/goat/extn.rb

Instance Method Summary collapse

Instance Method Details

#fetch_class(str) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/goat/extn.rb', line 67

def fetch_class(str)
  path = str.to_s.split('::')
  from_root = path[0].empty?
  if from_root
    from_root = []
    path = path[1..-1]
  else
    start_ns = ((Class === self)||(Module === self)) ? self : self.class
    from_root = start_ns.to_s.split('::')
  end
  until from_root.empty?
    begin
      return (from_root+path).inject(Object) { |ns,name| ns.const_get(name) }
    rescue NameError
      from_root.delete_at(-1)
    end
  end
  path.inject(Object) { |ns,name| ns.const_get(name) }
end