yielding a block to another block

  • Thread starter David Chelimsky
  • Start date
D

David Chelimsky

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
 
E

Eero Saynatkari

David said:
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
 
E

Eero Saynatkari

Eero said:
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
 
D

David Chelimsky

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

# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top