返回首页 - Notes - 2014

Ruby Yield Block Proc Lambda


Yield

def call_block
  yield 1
  yield 2
  yield 3
end

call_block { |i| puts i }

Block

def call_block (&block)
  block.call 1
  block.call 2
  block.call 3
end

call_block { |i| puts i }

Proc

def call_block (&block)
  block.call 1
  block.call 2
  block.call 3
end

p = Proc.new { |i| puts i }

call_block &p

Lambda

def call_block (&block)
  block.call 1
  block.call 2
  block.call 3
end

p = ->i { puts i }

call_block &p

date : 2014-12-16