How can I invoke the 'include' in the class instance function


#1

For same reason, I want to invoke the ‘include’ in the class instance
function.
The code like this:


class QQ
module QQ_Elem
def print_elem
puts “elem”
end
end

def test
include QQ_Elem
print_elem
puts “test”
end
end

qq = QQ.new
qq.test


But I got the error like this:
in test': undefined methodinclude’ for #QQ:0x2bb5150
(NoMethodError)

Is there anyone know the solutione$B!)e(B
Thank you


#2

On Nov 19, 2008, at 15:56, Jarod Z. wrote:

But I got the error like this:
in test': undefined methodinclude’ for #QQ:0x2bb5150
(NoMethodError)

Call it on the class instead, as in self.class.include QQ_Elem.

However, since it’s only supposed to be called from within the class
itself (not an instance of the class), it’s marked as private. To
circumvent this, call self.class.send :include, QQ_Elem.


#3

Jarod Z. wrote:

For same reason, I want to invoke the ‘include’ in the class instance
function.

class QQ
module QQ_Elem
def print_elem
puts “elem”
end
end

def test
extend QQ_Elem # <-----------
print_elem
puts “test”
end
end

qq = QQ.new
qq.test #=> elem
# test