Im

Im is a (currently) experimental implementation of import modules in Ruby, similar to systems like CommonJS, ESmodules, and RequireJS in Javascript. It requires a patched version of Ruby which you will need to build in order to use the gem.

Installation

Im requires a patched version of Ruby to use; if not installed, the gem will raise and exit.

You can find the patched version of Ruby here:

https://github.com/shioyama/ruby/tree/import_modules

Once installed, install the gem and the error should go away.

Usage

Extend Im and use import to import files or gems under an anonymous module namespace:

```ruby require “im”

extend Im

mod = import “active_model” #=> <#Im::Import root: active_model> ```

Constants in the imported files are under the returned module, not the root namespace:

```ruby ActiveModel #=> uninitialized constant ActiveModel (NameError)

mod::ActiveModel #=> ActiveModel ```

You can now assign your custom parent namespace and use the gem constants as always:

```ruby MyRails = mod

class EmailContact include MyRails::ActiveModel::API

attr_accessor :name validates :name, presence: true end

contact = EmailContact.new contact.valid? #=> false

contact.name = “foo” contact.valid? #=> true ```

License

The gem is available as open source under the terms of the MIT License.