Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class instance into included module

Reply
Thread Tools

Class instance into included module

 
 
Galevsky Gal
Guest
Posts: n/a
 
      06-26-2007
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'

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
John Wilger
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 10:00 am, Galevsky Gal <galev...@gmail.com> wrote:
> here is my problem: I would like to handle class instances into a module
> included by the class. This case is illustrated below:


<snip>

> 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

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      06-26-2007


On Jun 26, 1:00 pm, Galevsky Gal <galev...@gmail.com> wrote:
> 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 ?


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.


 
Reply With Quote
 
Galevsky gal
Guest
Posts: n/a
 
      06-26-2007
Trans wrote:
> On Jun 26, 1:00 pm, Galevsky Gal <galev...@gmail.com> 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'

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Galevsky gal
Guest
Posts: n/a
 
      06-26-2007
But what I think I'll do is make TestModule a class, and my TestAClass
class will inherit from TestModule.

Gal'

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      06-26-2007
On 6/26/07, Galevsky gal <> 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


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

 
Reply With Quote
 
Galevsky gal
Guest
Posts: n/a
 
      06-26-2007
Robert Dober wrote:
> On 6/26/07, Galevsky gal <> 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 )

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'

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      06-26-2007
On 6/26/07, Galevsky gal <> 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
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

 
Reply With Quote
 
Galevsky gal
Guest
Posts: n/a
 
      06-26-2007
Robert Dober wrote:

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

<snip>
> def self.extended other


You got it !!!

Many thanks my friend ;o)

Gal'

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Including a module in an included module. Prince Nez Ruby 3 08-18-2009 11:46 AM
Calling instance method when module included Leon Bogaert Ruby 0 05-28-2008 09:01 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57