creature = Creature.new()
creature.extend( Vampiric )
# some time passes
creature.remove( Vampiric ) # doesn't work - how do I do it?
module Cat
def mew; "Mew!"; end
end
module Lion
def roar; "Roar!"; end
end
class LionCat < Lion;
include Cat
def RoarMew; "Roarmew!"; end
end
kitty = LionCat.new
kitty.roar # => "Roar!"
kitty.mew # => "Mew!"
kitty.RoarMew # => "Roarmew!"
kitty.ancestors.inspect # Lion and Cat are both shown as ancestors.
creature = Creature.new()
creature.extend( Vampiric )
# some time passes
creature.remove( Vampiric ) # doesn't work - how do I do it?
require 'inline'
class Object
inline(:C) do |builder|
builder.c <<-EOC
void unextend(VALUE module) {
VALUE klass = rb_singleton_class(self);
VALUE prev_klass = Qnil;
while (klass) {
if (klass == module || RCLASS(klass)->m_tbl == RCLASS(module)->m_tbl) {
RCLASS(prev_klass)->super = RCLASS(klass)->super;
rb_clear_cache();
return;
}
prev_klass = klass;
klass = RCLASS(klass)->super;
}
}
EOC
end
end
module Vampiric
def bite
puts "chomp"
end
end
class Creature
end
a = Creature.new
begin
a.bite
rescue
puts "no bite"
end
a.extend Vampiric
a.bite
a.unextend Vampiric
begin
a.bite
rescue
puts "no bite"
end
module One; def roar; p "Roar!"; end; end
class Two; end
c = Two.new.extend(One).roar
c = c.dup
begin
c.roar
rescue
p "Fail."
end
>sudo gem install un
Successfully installed un-1.0.1
1 gem installed
Installing ri documentation for un-1.0.1…
Updating class cache with 1689 classes…
Installing RDoc documentation for un-1.0.1…
require 'un'
module One; def roar; p "Roar!"; end; end
class Two; end
c = Two.new.extend(One).roar
c.unextend(One)
begin
c.roar
rescue
p "Fail."
end
I've sifted through a lot of docs, but I can't find a way to do this, or if it's even possible.