Sha256: 907ea27c968580569a2cb6e163c9987d0ed5cf40c2ab563721603af634fb1f02
Contents?: true
Size: 1.06 KB
Versions: 50
Compression:
Stored size: 1.06 KB
Contents
# From http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html # Adapted into a module, rather than monkey patching it into Kernel # # Method to take a string constant name, including :: qualifications, and # look up the actual constant. Looks up relative to current file. # Respects leading ::. Etc. # # class Something # include Traject::QualifiedConstGet # # def foo # #... # klass = qualified_const_get("Foo::Bar") # #... # end # end module Traject::QualifiedConstGet def qualified_const_get(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 end
Version data entries
50 entries across 50 versions & 1 rubygems