Hi all, hope everybody had a great new year - it is snowing outside for
the first time this winter
Sorry if this is common knowledge, I have done some searching of archives
but cannot find information…
Is there a way to tell whether or not a method takes a block argument?
The sort of thing that I want to do is write the Client class below (from
the pseudocode example given):
class Server
def methodWithNoBlock(arg)
return 1
end
def methodWithBlock(arg, &blk)
blk.call(1)
blk.call(2)
end
end
Client is the class that I will write - sort of a delegator with knobs
on
client = Client.new(Server.new)
If you call the method “normally” - then everything behaves as expected
client.methodWithNoBlock => 1
If you call the method with a block and it doesn’t take a block, then
the method’s return value
is passed as block argument.
client.methodWithNoBlock { |arg|
# here arg is 1, and the method returns nil
}
If you call a method that takes a block, and you don’t pass one, then
the method’s return value
is an array of the block arguments:
client.methodWithBlock => [1, 2]
If you call a method that takes a block, and you do pass one, then
everything behaves as expected
client.methodWithBlock { |arg|
# first time arg is 1, second time it is 2
# method returns nil
}
The bit that I don’t know how to do is:
- If client detects that the requested method takes a block argument, it
should:
a) pass the block given to it (if there was one), or
b) create a dummy block that fills a return array, because the caller
didn’t provide a block.
Possible? Sensible? etc.
Cheers for any help you can give,
···
–
Martin Hart Tel: +44 (0) 1582 618468
Arnclan Fax: +44 (0) 1582 619596
Union Street, E-mail: martin@zsdfherg.com
Dunstable, Beds LU6 1EX