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

J

Jarod Zhu

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 method `include' for #<QQ:0x2bb5150>
(NoMethodError)

Is there anyone know the solution$B!)(B
Thank you
 
M

Mikael Høilund

But I got the error like this:
in `test': undefined method `include' 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 =20
itself (not an instance of the class), it's marked as private. To =20
circumvent this, call `self.class.send :include, QQ_Elem`.

--=20
Name =3D "Mikael H=F8ilund"; Email =3D Name.gsub %r/\s/,%#=3D?,#
*a=3De=3D?=3D,!????,:??,?,,Email.downcase![eval(%["\\%o\\%o"]%
[?**2+?o,?\\*2])]=3D"o";Email.gsub! %%\%c%*3%a, %?%c? % ?@
def The(s)%%\%s.%%s+%.org\n.end; :Go and print The Email
 
M

Mike Gold

Jarod said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top