From: Brian Candler Date: 2011-04-19T00:53:08+09:00 Subject: Re: To Yield or Not to Yield: An Inferable Question 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/.