[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

yielding a block to another block

David Chelimsky

9/2/2006 7:08:00 PM

Hi all,

The mocking framework in rspec uses method_missing to capture all the
messages sent to it, comparing the message to a list of expected
messages. For various reasons I'm trying to rework this so that
instead of using method_missing, we define the expected message on the
mock on the fly.

For that to happen, setting expectations on the mock ends up invoking this:

def define_expected_method(sym)
(class << self; self; end).__send__(:define_method, sym,
lambda {|*args| message_received(sym, *args)})
end

The Proc yields *args, which are then passed to message_received. How
can I get the Proc to yield a block that is passed in with the call to
:sym? I tried this:

lambda {|*args, &block| ....

but it throws an error. Any other ideas? Let me know if the question
is not clear.

Thanks,
David

4 Answers

e

9/2/2006 8:09:00 PM

0

David Chelimsky wrote:
> Hi all,
>
> The mocking framework in rspec uses method_missing to capture all the
> messages sent to it, comparing the message to a list of expected
> messages. For various reasons I'm trying to rework this so that
> instead of using method_missing, we define the expected message on the
> mock on the fly.
>
> For that to happen, setting expectations on the mock ends up invoking
> this:
>
> def define_expected_method(sym)
> (class << self; self; end).__send__(:define_method, sym,
> lambda {|*args| message_received(sym, *args)})
> end
>
> The Proc yields *args, which are then passed to message_received. How
> can I get the Proc to yield a block that is passed in with the call to
> :sym? I tried this:
>
> lambda {|*args, &block| ....
>
> but it throws an error. Any other ideas? Let me know if the question
> is not clear.

You cannot currently do that with a block (1.9 and 2.0 will
allow &block arguments). In the meanwhile, you can #eval the
method into existence instead:

def define_expected_method(sym)
class << self; self; end.__send__ :class_eval, %{
def #{sym}(*args, &block)
message_received sym, *args, &block # ?
end
}
end

> Thanks,
> David


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

e

9/2/2006 8:14:00 PM

0

Eero Saynatkari wrote:
> David Chelimsky wrote:
>> Hi all,
>>
>> The mocking framework in rspec uses method_missing to capture all the
>> messages sent to it, comparing the message to a list of expected
>> messages. For various reasons I'm trying to rework this so that
>> instead of using method_missing, we define the expected message on the
>> mock on the fly.
>>
>> For that to happen, setting expectations on the mock ends up invoking
>> this:
>>
>> def define_expected_method(sym)
>> (class << self; self; end).__send__(:define_method, sym,
>> lambda {|*args| message_received(sym, *args)})
>> end
>>
>> The Proc yields *args, which are then passed to message_received. How
>> can I get the Proc to yield a block that is passed in with the call to
>> :sym? I tried this:
>>
>> lambda {|*args, &block| ....
>>
>> but it throws an error. Any other ideas? Let me know if the question
>> is not clear.
>
> You cannot currently do that with a block (1.9 and 2.0 will
> allow &block arguments). In the meanwhile, you can #eval the
> method into existence instead:
>
> def define_expected_method(sym)
> class << self; self; end.__send__ :class_eval, %{
> def #{sym}(*args, &block)
> message_received sym, *args, &block # ?
> end
> }
> end

# One of these days.. *sigh*
def define_expected_method(sym)
class << self; self; end.__send__ :class_eval, %{
def #{sym}(*args, &block)
message_received :#{sym}, *args, &block # ?
end
}
end

>
>> Thanks,
>> David

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

David Chelimsky

9/2/2006 8:17:00 PM

0

Thank you! Can I credit you when I commit this?

On 9/2/06, Eero Saynatkari <eero.saynatkari@kolumbus.fi> wrote:
> Eero Saynatkari wrote:
> > David Chelimsky wrote:
> >> Hi all,
> >>
> >> The mocking framework in rspec uses method_missing to capture all the
> >> messages sent to it, comparing the message to a list of expected
> >> messages. For various reasons I'm trying to rework this so that
> >> instead of using method_missing, we define the expected message on the
> >> mock on the fly.
> >>
> >> For that to happen, setting expectations on the mock ends up invoking
> >> this:
> >>
> >> def define_expected_method(sym)
> >> (class << self; self; end).__send__(:define_method, sym,
> >> lambda {|*args| message_received(sym, *args)})
> >> end
> >>
> >> The Proc yields *args, which are then passed to message_received. How
> >> can I get the Proc to yield a block that is passed in with the call to
> >> :sym? I tried this:
> >>
> >> lambda {|*args, &block| ....
> >>
> >> but it throws an error. Any other ideas? Let me know if the question
> >> is not clear.
> >
> > You cannot currently do that with a block (1.9 and 2.0 will
> > allow &block arguments). In the meanwhile, you can #eval the
> > method into existence instead:
> >
> > def define_expected_method(sym)
> > class << self; self; end.__send__ :class_eval, %{
> > def #{sym}(*args, &block)
> > message_received sym, *args, &block # ?
> > end
> > }
> > end
>
> # One of these days.. *sigh*
> def define_expected_method(sym)
> class << self; self; end.__send__ :class_eval, %{
> def #{sym}(*args, &block)
> message_received :#{sym}, *args, &block # ?
> end
> }
> end
>
> >
> >> Thanks,
> >> David
>
> --
> Posted via http://www.ruby-....
>
>

e

9/2/2006 11:38:00 PM

0

David Chelimsky wrote:
> Thank you! Can I credit you when I commit this?

Certainly

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