Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Experiment to implement granular loading of module code in Ruby
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
VERSION
including.gemspec

README.rdoc

including

  • An observation: Other languages allow for more granular loading of only desired methods and functions from module code.

  • An experiment: How might this be done in Ruby?

  • A warning: Running this code in production might not be wise.

How does it work?

Let's say you've got a module:

module Foo
  def foo
    "foo"
  end

  def bar
    "bar"
  end

  def baz
    "baz"
  end
end

And you want to mix it in to a class. But wait! the `foo` method might conflict with some other `foo`!

class Bar
  including Foo, except: "foo"
end

bar = Bar.new
bar.bar # "bar"
bar.foo # NoMethodError

That's basically it. You can pass an array of `only` the methods you'd like to mix in, or an array of exceptions, via `except`.

class Quux
  including Foo, only: [:foo, :baz]
end

quux = Quux.new
quux.foo # "foo"
quux.bar # NoMethodError
quux.baz # "baz"

Installation

gem install including

Copyright

Copyright © 2012 Bryan Woods. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.