object2module

Created: 2008-12-03 07:00
Updated: 2016-05-08 18:21
License: mit

README.markdown

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:

  • Remix - Makes ancestor chains read/write
  • Include Complete - Brings in module singleton classes during an include. No more ugly ClassMethods and included() hook hacks.
  • Prepend - Prepends modules in front of a class; so method lookup starts with the module
  • GenEval - A strange new breed of instance_eval
  • LocalEval - instance_eval without changing self

Contact

Problems or questions contact me at github

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more