On 4/30/07, Robert K. [email protected] wrote:
in the calling method.
from :0
irb(main):008:0> f {|a,b,c,d| p a,b}
0
1
=> nil
Kind regards
robert
It’s a bit more complicated than that, but that’s the idea. If you’re
curious, the library is called Handshake (handshake.rubyforge.org;
haven’t made an announcement as it’s not quite mature yet). It
supports (among other things) argument contracts of the form:
contract String => Integer
def to_i …
contract “foo” => 1…3
def foo …
contract any?( String, hash_of?(Symbol, Fixnum) ) => String
def accepts_string_or_hash …
The goal is to extend it to support block contracts:
contract [ String, Block(String => Integer) ] => Integer
The Handshake library surrounds an object that includes the
appropriate module with a proxy object and checks everything that
passes across that barrier. That means that I can easily manipulate
any incoming Proc objects, extending the instance so that Proc#call is
required to check an argument list. The problem is that changing the
behavior of Proc#call does not affect the behavior of yield:
class Proc
def weird(*args)
puts “weird call!” if args.length > 0 && args[0] == “weird”
orig_call(*args)
end
alias :orig_call :call
alias :call :weird
end
def weird_call(str, &block)
block.call(str)
end
def weird_yield(str)
yield(str)
end
In IRB:
weird_call(“weird”) { true }
weird call!
=> true
weird_yield(“weird”) { true }
=> true
Cheers,
Brian