From: Martin Hart Date: 2003-01-09T00:15:57+09:00 Subject: Re: Can you detect if a method takes a block argument? Wow - thanks for all the responses, I'm overwhelmed with information now :-) Ok, my design needs rethinking, but Martin Weber's code snippet made me think a bit. I want a program that will run in "distributed" or "local" mode without changing any code. While this is fairly straightforward to achieve in Ruby (delegation with an optional XMLRPC layer or some such), I also need to optimise various types of method call. For example: A "server" has a method loadAllItems - (there could be lots of items). A "client" calls it. Now in a "local" environment, it might be ok for the client to wait for the server to build a huge array of items and return it. In the "distributed" environment it probably won't be. So, what I thought I would do is: server.loadItems(loadCriteria) { |item| # Do something with item (perhaps the block receives a chunk of items rather than just 1) } By using delegation, this will work regardless of where the "server" is (same process or other side of world). So to answer Guy's question: > You really *always* want to call Array#sort with a block ? In the case of my design (if you can call it that :-), the block doesn't really have any semantics associated with the message call - it's just an alternative way to receive the method's return value/or partial return value. [(D Black)] >Can you not just document your methods so that the code that invokes them knows what to do? Yes - - loadAllItems never takes a block and returns the lot - loadItemsInChunks can always take a block pass it a chunk of items - In distributed mode, there is a proxy layer, in local mode there isn't. Perhaps I am trying to be too clever? What I think I want is to function "sensibly" regardless of whether a block is passed. My definition of sensible is obviously different from everyone elses :-) I have been persuaded that the design is bad - I'll rethink. Finally, on the topic of snow, 1. Its cold and wet 2. Its too bright to look at 3. Bah, Humbug :-) Cheers, Martin