Hello,

At 03:44 07/06/2002 +0900, you wrote:
>Is it possible to get the same effect as the following, but without
>having to pass self as an arguement?
>-------------------------------------------
>class TestClass
>         def testMethod (calling_object)
>                 puts calling_object.type
>         end
>end
>
>class AnotherClass
>         def do
>                 TestClass.new.testMethod(self)
>         end
>end
>
>AnotherClass.new.do
>This should output "AnotherClass".

Do you need to know about the calling object or do you intend it to
do something for you ? Anyways...

I had a similar issue to solve recently and it seems that "block" &
eval() may help here:

class TestClass
   def testMethod( &block )
     r = my_test_result
     puts eval( "self", block).type  # "self" where block was defined
   rescue Exception ex; r = ex
   ensure
     block.call( r)                  # Callback to signal test result
   end
end

class AnotherClass
   def do
     TestClass.new.testMethod() do | r |
       # Process test result r here
     end
   end
end

Warning: Not tested.

Depending on what you need, it is either the ability to get "self"
from a block or using a callback that may help you.

BTW: We are missing a caller() that would include bindings. With it
one could write: puts eval( "self", caller( :Bindinds)[0]).type; to
achieve exactly what you want.

Yours,

Jean-Hugues

-------------------------------------------------------------------------
Web:  http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17