banister / object2module

Convert Classes and Objects to Modules so they can be extended/included.
MIT License
9 stars 0 forks source link

Object2module

(C) John Mair (banisterfiend) 2010

Enables Classes and Objects to be mixed into ancestor chains

Using Object2module you can treat classes and object (their singletons, anyway) as if they were modules and include or extend them into ancestor chains.

Object2module provides the gen_include and gen_extend methods which are generalizations of the traditional include and extend.

example: gen_include()

Using gen_include we can include a class into another class:

class C
  def hello
    :hello
  end
end

class D
  gen_include C
end

D.new.hello #=> :hello
D.ancestors #=> [D, C, Object, ...]

example: gen_extend()

gen_extend lets us mix objects into objects:

o = Object.new
class << o
  def bye
    :bye
  end
end

n = Object.new
n.gen_extend o
n.bye #=> :bye

How it works

Object2module removes the check for T_MODULE from rb_include_module() and prevents inclusion of classes above Object (or meta^n(Object) for singleton classes).

Companion Libraries

Object2module is one of a series of experimental libraries that mess with the internals of Ruby to bring new and interesting functionality to the language, see also:

Contact

Problems or questions contact me at github