On Apr 1, 2005 4:41 PM, Andrew Walrond <andrew / walrond.org> wrote:
> On Saturday 02 April 2005 00:34, Andrew Walrond wrote:
> > My final solution, taking the above into account:
> >
> > c.rb
> >  class Glibc
> >  end
> >
> > b.rb
> >  inline 'c.rb'
> >
> >  class Gcc
> >  end
> >
> > a.rb
> >  module Packages
> >    def Packages.inline(filename)
> >      eval(IO.readlines(filename).join,nil,filename)
> >    end
> >    inline('c.rb')
> >  end
> >
> >  Packages::Gcc.new
> >  Packages::Glibc.new
> >
> 
> My final puzzle before sleep; How to throw an exception if any of the package
> files try to use require() or load() instead of inline() ? (My package
> developers might forget and pollute the global namespace)
> 
> Is there a neat way?
> 
> Andrew

This should give you the general idea if you want to prevent it.

module Kernel
  alias _load load
  def load(file, wrap)
    raise "..." if check_if_f_is_a_package_file
    _load(file, wrap)
  end
  
  alias _require require
  def require(lib)
    raise '...' if check_if_lib_plus_rb_is_a_package_file
    _require(lib)
  end
end

not sure if this covers everything you want. You may want to disable
require and load completely while you are in your eval:

# Not yet thread safe but can be made so.

module Packages
  def Packages.disable_loading(&block)
    Kernel.module_eval do
      alias _load load
      def load(*); raise '??' end
      alias _require require
      def require(*); raise '??' end
    end
    ret = yield
    Kernel.module_eval do
      alias load _load
      alias require _require
    end
    ret
  end

  def Packages.inline(filename)
    disable_loading {
      eval(IO.readlines(filename).join, nil, filename)
    }
  end

  inline('c.rb')
end

Brian.