Skip to content
Explicit imports for Ruby on a per-class/module basis
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



This module adds explicit imports to Ruby. By calling the import method during class or module definition, all constants in the module's namespace will be replaced by placeholders, except a few core types and those that are explicitly imported. For example, in this class:

class Joe
  import 'File'

  def file_exists?(path)

  def create_queue

the File module will be available:

=> true

But other constants, like Queue, won't be:

NoMethodError: undefined method `new' for #<ExplicitImport::CantTouchThis:0x10135a8c0>

As you can see, the constants are present, but have placeholder values:

>> Joe.const_get(:Queue)
=> #<ExplicitImport::CantTouchThis:0x10135a8c0>

That's unfortunate. I have an idea about how to make them actually throw a NameError, though it's pretty evil.


Minimal. You almost certainly don't want to use this as it exists right now. For one thing, it only isolates the module from constants defined before the module itself; subsequent constant definitions will be missed.

There are probably ways to fix that. Patches are welcome if this kind of thing tickles your fancy.

Something went wrong with that request. Please try again.