A scoped module import system for Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.rspec
.ruby-version
Gemfile
Gemfile.lock
README.md
Rakefile
module_require.gemspec

README.md

Module Require

A scoped, module based code loading mechanism for Ruby

This is an experimental project to ascertain the utility and practicality of an opt-in, scoped code loading mechanism.

require evaluates the target Ruby code within the global namespace creating new constants or modifying existing ones.

module_require aims to provide a mechanism for loading files into modules such that imported code is scoped, preventing unintentional pollution of the global namespace and allowing the user more control over dependencies.

Features

  • Load two versions of the gem
  • Load a gem into an anonymous module such that access to it can be private
  • Whitelist top level constants

Caveats

Ruby offers no real safety or restrictions so any of these features can of course be circumvented as simply as using absolute constant lookup eg ::MyModule or Object::MyModule.

Goals

  • Work without VM modifications
  • Compatibility with existing popular libraries
  • Code that is approachable

Example

module MyTopLevelModule
  module_require "some_library"
end

::Object.constants.include?(:SomeLibrary)
# => false

MyTopLevelModule.constants.include?(:SomeLibrary)
# => true