Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3104

Re: To Yield or Not to Yield: An Inferable Question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail
From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: To Yield or Not to Yield: An Inferable Question
Date Mon, 18 Apr 2011 10:53:08 -0500
Organization Service de news de lacave.net
Lines 38
Message-ID <39511275f4c073ddb3fc19287e9b4c5e@ruby-forum.com> (permalink)
References <CA1B8DEB-7979-4D1B-8594-3BA3C72AE76F@carboni.ca> <c0656e03bb64f14a6b7f3ac7a3ab69e5@ruby-forum.com> <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 1303142009 77427 65.111.164.187 (18 Apr 2011 15:53:29 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Mon, 18 Apr 2011 15:53:29 +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 381783
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <39511275f4c073ddb3fc19287e9b4c5e@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3104

Show key headers only | View raw


Michael Edgar wrote in post #993395:
>> 1. In my experience, very little real-world Ruby code uses
>> 'block_given?'. If it needs to yield, it just yields. I'd consider this
>> to be a case of duck-typing.
>
> This seems to suggest Rubyists rarely write methods that take blocks
> optionally. Of this, I am highly skeptical.

Ah, by "optionally" I think you mean "does one thing when a block is 
given, but something else when a block is not given". Now I think some 
more, there is a fairly common case:

class MyFile
  def self.open(*args)
    file = open_it(*args)
    if block_given?
      begin
        yield file
      ensure
        file.close
      end
    else
      return file
    end
  end
end

Code analysis can tell you that it's OK to call the method either with 
or without a block (at least assuming no pathological use cases, like 
redefining 'block_given?')

Regards,

Brian.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-14 12:47 -0500
  Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-17 15:22 -0500
    Re: To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-17 16:40 -0500
      Re: To Yield or Not to Yield: An Inferable Question Ryan Davis <ryand-ruby@zenspider.com> - 2011-04-18 03:41 -0500
      Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-18 10:53 -0500
      Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-20 03:59 -0500
        Re: To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-20 16:46 -0500
          Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-21 04:47 -0500
            Re: To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 06:18 -0500
              Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-21 12:55 -0500
  Re: [ANN] To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-18 04:34 -0500
    Re: [ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-18 04:46 -0500
      Re: [ANN] To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-18 06:16 -0500
        Re: [ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-18 07:26 -0500

csiph-web