Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail From: Brian Candler Newsgroups: comp.lang.ruby Subject: Re: To Yield or Not to Yield: An Inferable Question Date: Wed, 20 Apr 2011 03:59:12 -0500 Organization: Service de news de lacave.net Lines: 54 Message-ID: <157ee9d8e95b68bee86be7ba51ba49f1@ruby-forum.com> References: <78681B86-AB88-4DA6-A8AA-EEA59186B407@carboni.ca> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1303291957 62573 65.111.164.187 (20 Apr 2011 09:32:37 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Wed, 20 Apr 2011 09:32:37 +0000 (UTC) In-Reply-To: <78681B86-AB88-4DA6-A8AA-EEA59186B407@carboni.ca> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381906 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <157ee9d8e95b68bee86be7ba51ba49f1@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3225 Michael Edgar wrote in post #993395: > If one peruses the Ruby standard library, one will find that just in the > Ruby > code alone, block_given? occurs 265 times, in *every single case* is > used > to execute yield conditionally, and in every single case, the result is > used > only as a simple constant. [4] However there are some cases where this is done unnecessarily, net/telnet.rb being the prime example. e.g. if block_given? waitfor({"Prompt" => match, "Timeout" => time_out}){|c| yield c } else waitfor({"Prompt" => match, "Timeout" => time_out}) end could have been written simply as: waitfor({"Prompt" => match, "Timeout" => time_out}, &blk) Net::Telnet also has a load of conditionals because it lets you pass an optional block to each call for capturing debug information - an awkward API to use, because often you end up passing the same block every time. It would have been much easier to pass this in the options hash where it could have been set as a default. e.g. t = Net::Telnet.new("Debug" => lambda { |c| print c }, ...) t.cmd("foo") t.cmd("bar") t.cmd("baz") whereas as the moment you have to write t = Net::Telnet.new(...) out = lambda { |c| print c } t.cmd("foo",&out) t.cmd("bar",&out) t.cmd("baz",&out) Also, a Debug parameter could invoke the "<<" method instead of "call", which would make it usable with Files and Strings. Then Proc#<< could be aliased to call, and duck-typing would suddenly become a lot prettier. There would also be no need for Enumerator::Yielder either. Sorry, I've strayed right off there :-) -- Posted via http://www.ruby-forum.com/.