0.01
No commit activity in last 3 years
No release in over 3 years
object2module enables ruby classes and objects to be used as modules
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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