# Class instance into included module

**URL:** <https://www.ruby-forum.com/t/class-instance-into-included-module/107840>\
**Category:** Ruby\
**Created:** [June 26, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840 "2007-06-26T19:00:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/1 "2007-06-26T19:00:23Z")

</div>

Hi all,

here is my problem: I would like to handle class instances into a module  
included by the class. This case is illustrated below:

module Mod  
instanceOfClas = ???  
method\_HelloWorld = instanceOfClas.method(‘helloWorld’)

# do something with the method that could be:

method\_HelloWorld.call if iwant  
end

class Clas  
include Mod

def helloWorld  
“Hello world !”  
end  
end

Do you have any idea ? I cannot get my instance using self since self  
means the module itself. How can I make a .call onto _parent_ class  
method ?

Many thanks for your support,

Gal’

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/2 "2007-06-26T19:11:05Z")

</div>

On Jun 26, 10:00 am, Galevsky G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> here is my problem: I would like to handle class instances into a module  
> included by the class. This case is illustrated below:

> Do you have any idea ? I cannot get my instance using self since self  
> means the module itself. How can I make a .call onto _parent_ class  
> method ?

Is Module#included what you want, perhaps?  
[http://ruby-doc.org/core/classes/Module.html#M001683](http://ruby-doc.org/core/classes/Module.html#M001683)

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/3 "2007-06-26T19:14:06Z")

</div>

On Jun 26, 1:00 pm, Galevsky G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> method\_HelloWorld.call if iwant  
> Do you have any idea ? I cannot get my instance using self since self  
> means the module itself. How can I make a .call onto _parent_ class  
> method ?

The way you have it paradoxical b/c “method\_HelloWorld.call if iwant”  
is happening before an instance is ever created. BUt if you just men  
accessing Clas, then just put that:

method\_HelloWorld = Clas.instance\_method(‘helloWorld’)

However, Ruby has some limitations, in that a method needs to be bound  
to an instance. And binding is restricted to subclasses of the type of  
class the method was taken from. Unfortunate that. But usually one can  
find a way to manage. In your case you should ask "what am I trying to  
call an instance method outside of an instance? Maybe it should be a  
module method, which the instance can call upon instead.

T.

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/4 "2007-06-26T19:57:09Z")

</div>

But what I think I’ll do is make TestModule a class, and my TestAClass  
class will inherit from TestModule.

Gal’

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/5 "2007-06-26T19:51:13Z")

</div>

Trans wrote:

> On Jun 26, 1:00 pm, Galevsky G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > method\_HelloWorld.call if iwant  
> > Do you have any idea ? I cannot get my instance using self since self  
> > means the module itself. How can I make a .call onto _parent_ class  
> > method ?
> 
> The way you have it paradoxical b/c “method\_HelloWorld.call if iwant”  
> is happening before an instance is ever created. BUt if you just men  
> accessing Clas, then just put that:
> 
> method\_HelloWorld = Clas.instance\_method(‘helloWorld’)
> 
> However, Ruby has some limitations, in that a method needs to be bound  
> to an instance. And binding is restricted to subclasses of the type of  
> class the method was taken from. Unfortunate that. But usually one can  
> find a way to manage. In your case you should ask "what am I trying to  
> call an instance method outside of an instance? Maybe it should be a  
> module method, which the instance can call upon instead.
> 
> T.

Thanks for your posts guys ;o)

Well, I am writting a light unit test framework, like Junit for java.

So, I have a TestModule module that deals with Tests objects, assertions  
and so on. The behaviour I want is:

Take a class:

Class AClass  
def method1  
“hello”  
end  
end

To unit-test it, I would like to just need to write that:

Class TestAClass  
def TestMethod1  
assume(AClass.new.method,“hello”)  
end

include TestModule  
end

To do that, my TestModule should be…

module TestModule

class TestManager

# handle a tests register, launches all the methods, handles the

results, display info and so on

def register(method)  
@register \<\< method  
end

def launch  
@register.each{ |test|  
test.call  
}  
end

def testsSucceed?  
# handle lots stuff with assume() calls executed during the launch  
…  
end  
end

# find all the methods of TestAClass that have ‘Test’ in their name,

and add them into the register handled by the TestManager

mgr = TestManager.new  
testing\_instance = #here is my pb and I 'd like to get a TestAClass  
instance  
testing\_instance.methods.each{ |method|  
if !(method =~ /Test/).nil?  
mgr.register(testing\_instance.method(method))  
end  
}  
puts “launch All the registered tests”  
mgr.launch

end

I give you a shortened version but the necessary part to understand what  
I would like to do.

Thanks for your attention,

Gal’

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/6 "2007-06-26T21:23:32Z")

</div>

Robert D. wrote:

> On 6/26/07, Galevsky gal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 
> 
> If I am not mistaken Test::Unit does instantiate an object of all  
> classes that extend Test::Unit::TestCase ( by exploring ObjectSpace in  
> an et\_exit handler ) than calls setup,x,teardown for all x starting  
> with test.
> 
> Maybe you can adopt something from this approach actually what you  
> said above is basically the same, right?
> 
> Just out of curiosity, are you unhappy with Test::Unit or are you  
> doing this for fun – which is a _very good reason_ BTW 🙂
> 
> Cheers  
> Robert

for fun :o)

I do it now with a class extension instead of module inclusion due to  
limitations, even if mixin best suits the needs INMHO.

Many thanks, folks !

Gal’

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/7 "2007-06-26T21:08:24Z")

</div>

On 6/26/07, Galevsky gal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

If I am not mistaken Test::Unit does instantiate an object of all  
classes that extend Test::Unit::TestCase ( by exploring ObjectSpace in  
an et\_exit handler ) than calls setup,x,teardown for all x starting  
with test.

Maybe you can adopt something from this approach actually what you  
said above is basically the same, right?

Just out of curiosity, are you unhappy with Test::Unit or are you  
doing this for fun – which is a _very good reason_ BTW 🙂

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/8 "2007-06-26T22:15:31Z")

</div>

On 6/26/07, Galevsky gal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I do it now with a class extension instead of module inclusion due to  
> limitations, even if mixin best suits the needs INMHO.

Hmm I am quite sure Mixin will do the trick, why should we let you  
have all the fun 😉

module Tester  
def self.extended other  
registry =  
other.instance\_methods.select{|name|/^test/===name}.map{|mth|  
other.instance\_method mth}  
class \<\< other; self end.send :define\_method, :run\_tests do  
| msg |  
puts “running #{msg}”  
registry.each do |mth|  
mth.bind(other.new).call  
end  
end  
end  
end

class TestWhatever  
def test\_a; puts “I am tested” end  
extend Tester  
end

class TestSomethingElse  
def test\_b; puts “Me too” end  
extend Tester  
end

TestWhatever.run\_tests :whatever  
TestSomethingElse.run\_tests :something

Robert

---

<div class="post-metadata">

**Author:** ![galevsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/edb3f5/32.png) [@galevsky](https://www.ruby-forum.com/u/galevsky)\
**Post date:** [June 26, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/class-instance-into-included-module/107840/9 "2007-06-26T23:44:59Z")

</div>

Robert D. wrote:

\> \> Hmm I am quite sure Mixin will do the trick, why should we let you \> have all the fun ;) \> def self.extended other

You got it !!!

Many thanks my friend ;o)

Gal’
