From: David Masover Date: 2008-08-17T02:55:42+09:00 Subject: Re: More autoload... On Saturday 16 August 2008 07:33:22 Trans wrote: > It is a serious issue with one of > my programs. Because of it I am forced never to use autoload, and tell > all users of my program that they must do the same. I think I've found a solution, for myself -- basically a const_missing hack. Actually, I cracked open the ActiveSupport source, curious to see how they did it. And I spent probably an hour or two, staring at the code, before shaking my head and starting from scratch. Rails lets you write beautiful code, sometimes, but the Rails source sure isn't beautiful, most of the time. (In my not-so-humble opinion.) Still, I can't help but wonder -- if ActiveSupport had to do such backflips to get it working, maybe there's a serious assumption in my own code? Let me know if I'm missing something. I'm still trying to think of ways this could break. # You can use anything that provides an inflector. # I'm already using Sequel here, so it works for me. require 'sequel' class Module def const_missing_with_autoload(name) our_name = (self==Object) ? '' : self.name + '::' begin require (our_name+name.to_s).underscore const_get name rescue LoadError const_missing_without_autoload(name) end end alias_method :const_missing_without_autoload, :const_missing alias_method :const_missing, :const_missing_with_autoload end