From: "J.Hawkesworth" Date: 2003-01-08T22:56:57+09:00 Subject: Re: Can you detect if a method takes a block argument? Martin, I'm still new to ruby but I've noticed on http://dev.rubycentral.com/ref/ that there is a Kernel method called block_given? which I imagine might be of use to you, although it might require standing some of your logic on its head. Snowing here in Birmingham (UK) too (sorry to everyone in the rest of the world - snow is rare enough to be noteworthy to some of us weather-obsessed Brits). cheers, Jon -----Original Message----- From: Martin Hart [mailto:martin@zsdfherg.com] Sent: 08 January 2003 13:28 To: ruby-talk ML Subject: Can you detect if a method takes a block argument? 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: 1. 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 ________________________________________________________________________ This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk ________________________________________________________________________ _____________________________________________________________________ This message has been checked for all known viruses by Star Internet delivered through the MessageLabs Virus Control Centre. For further information visit http://www.star.net.uk/stats.asp _____________________________________________________________________ Any views or personal opinions expressed within this email may not be those of Talis Information Ltd. The content of this email message and any files that may be attached are confidential, and for the usage of the intended recipient only. If you are not the intended recipient, then please return this message to the sender and delete it. Any use of this e-mail by an unauthorised recipient is prohibited.