(C) John Mair (banisterfiend) 2010
Ruby modules remixed and remastered
Remix is a library to give you total control over class and module ancestor chains.
Using Remix you can add a module at any point in the chain, remove modules, replace modules, move modules around and otherwise 'remix' your modules.
gem install remix
Using temp_include
we can temporaliy mix in a module for the
duration of a block:
module M def hello() :hello end end
String.temp_include(M) do
puts "test".hello #=> "hello"
end
"test".hello #=> NoMethodError
Like the Mixico library Remix allows you to unextend (or uninclude) modules from inheritance chains; but also extends this functionality by (optionally) removing nested modules too:
C.ancestors #=> [C, A, B]
o = Object.new
o.extend C
o.singleton_class.ancestors #=> [C, A, B, Object, ...]
# remove entire nested module C by passing true as second parameter
o.unextend C, true
o.singleton_class.ancestors #=> [Object, ...]
Remix is intelligent enough to manipulate classes as well as modules:
class D < C
include M
end
D.ancestors #=> [D, M, C]
D.swap_modules C, M
D.ancestors #=> [D, C, M]
It does this by first converting all superclasses to Included Modules before remixing takes place.
Remix is a C-based extension that directly manipulates the superclass pointers of Included Modules.
Remix 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:
include-based functions:
extend-based functions:
Remix does not currently reorder the singleton classes of superclasses to reflect the new position of the class. This functionality is coming soon.
Problems or questions contact me at github
For Rue (1977-)