From: dblack@... Date: 2006-07-09T04:32:06+09:00 Subject: Re: Sweet Lord! Hi -- On Sun, 9 Jul 2006, Phil Tomson wrote: > On 7/8/06, William James wrote: >> Daniel Schierbeck wrote: >> > Why haven't I thought of this before? >> > >> > class Terminal >> > include Enumerable >> > >> > def each(&block) >> > while input = gets >> > block.call(input.chomp) >> > end >> > end >> >> def each >> while input = gets >> yield input.chomp >> end >> end > > Equivilent code, actually. It's a case of "you say tomaTOE I say tomAHto" > Personally, I tend to prefer the 'block.call' because you explicitly > say you're passing in a block in the definition of the method, so if > someone is reading your code they can easily tell that the method > takes a block. In the case of yield you might need to read through a > good bit of code before you find out that the method takes a block. It does seem that yielding is faster; note the effect of turning the block into a Proc object: require 'benchmark' include Benchmark n = 100000 def y end def b(&block) end bm do |x| x.report("call") { n.times { b { } } } x.report("yield") { n.times { y { } } } end # Output: user system total real call 1.860000 0.040000 1.900000 ( 2.874001) yield 0.220000 0.000000 0.220000 ( 0.249164) David -- "To fully realize the potential of Rails, it's crucial that you take the time to fully understand Ruby--and with "Ruby for Rails" David has provided just what you need to help you achieve that goal." -- DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS. Complete foreword & sample chapters at http://www.manning.com/black!