At Thu, 25 Apr 2002 03:00:33 +0900,
Ian Macdonald wrote:

> Is there any reason to prefer:
> 
>   def each(&block)
>     self.members.each(&block)
>   end
> 
> over:
> 
>   def each
>     self.members.each {|x| yield x}
>   end
> 
> ?
> 
> I'm wondering if these two approaches are always synonymous and
> whether there are any reasons to prefer one form over the other.

For simple tasks, &block passing looks faster than yield x. 
I took a benchmark:


  # begin of benchmark

  require "benchmark"

  class Dumb
    def initialize(size)
      @members = (0..size).to_a
    end

    def each_yield
      @members.each {|x| yield x}
    end

    def each_block(&block)
      @members.each(&block)
    end
  end

  dumb = Dumb.new(200000)
  a = b = 0

  Benchmark::bmbm(10) do |x|
    x.report("&block sum"){a = 0; dumb.each_block{|i| a+=i}}
    x.report("yield sum" ){b = 0; dumb.each_yield{|i| b+=i}}
    x.report("&block {}"){dumb.each_block{}}
    x.report("yield {}" ){dumb.each_yield{}}
  end

  puts "",a,b

  # end of benchmark


and got:


  Rehearsal ----------------------------------------------
  &block sum   1.015625   0.000000   1.015625 (  1.165606)
  yield sum    2.539062   0.000000   2.539062 (  2.632431)
  &block {}    0.062500   0.000000   0.062500 (  0.071375)
  yield {}     1.609375   0.000000   1.609375 (  1.933109)
  ------------------------------------- total: 5.226562sec

                   user     system      total        real
  &block sum   0.976562   0.000000   0.976562 (  1.240844)
  yield sum    2.539062   0.000000   2.539062 (  2.714653)
  &block {}    0.070312   0.000000   0.070312 (  0.072004)
  yield {}     1.601562   0.000000   1.601562 (  1.675738)

  20000100000
  20000100000


with ruby 1.7.2 (2002-04-24) [i386-freebsd4.5]. 

-- Gotoken