I
Intransition
I would like to use #include at an instance level, such that it
behaves just as it does at a class level. After a number of
experiments I thought for sure it would work if I ran the include
through the object's singleton. Alas, submodules remain inaccessible,
eg.
module M
def m1; "m1"; end
module N
def self.n1; "n1"; end
end
end
class X
def initialize(*mods)
(class << self; self; end).class_eval do
include *mods
end
end
def m ; m1 ; end
def n ; N.n1 ; end
end
x = X.new(M)
p x.m
p x.n #=> uninitialized constant X::N (NameError)
Is there any way to achieve this?
behaves just as it does at a class level. After a number of
experiments I thought for sure it would work if I ran the include
through the object's singleton. Alas, submodules remain inaccessible,
eg.
module M
def m1; "m1"; end
module N
def self.n1; "n1"; end
end
end
class X
def initialize(*mods)
(class << self; self; end).class_eval do
include *mods
end
end
def m ; m1 ; end
def n ; N.n1 ; end
end
x = X.new(M)
p x.m
p x.n #=> uninitialized constant X::N (NameError)
Is there any way to achieve this?