Yielding a block to another block

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

David C. 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

Eero S. wrote:

David C. 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

David C. wrote:

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

Certainly

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