Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > yielding a block to another block

Reply
Thread Tools

yielding a block to another block

 
 
David Chelimsky
Guest
Posts: n/a
 
      09-02-2006
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

 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      09-02-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      09-02-2006
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-forum.com/.

 
Reply With Quote
 
David Chelimsky
Guest
Posts: n/a
 
      09-02-2006
Thank you! Can I credit you when I commit this?

On 9/2/06, Eero Saynatkari <> 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-forum.com/.
>
>


 
Reply With Quote
 
Eero Saynatkari
Guest
Posts: n/a
 
      09-02-2006
David Chelimsky wrote:
> Thank you! Can I credit you when I commit this?


Certainly

--
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
C parser yielding syntax tree data structure? (Jamie Andrews) C Programming 7 04-22-2006 03:45 AM
Execute block in context of yielding method's module Trans Ruby 6 11-13-2005 05:13 PM
Yielding a chain of values Talin Python 18 09-10-2005 04:46 AM
Idiomatic conversion of yielding block to array David Brady Ruby 22 09-04-2005 07:51 PM
Ideas for yielding and exception raising Calvin Spealman Python 3 06-08-2004 10:59 AM



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