[ next ] [ prev ] [ contents ] Invitation To Ruby

How Open Uses Blocks

Using a block argument ...

  1: def myopen(filename, &block)
  2:   file = open(filename)
  3:   block.call(file)
  4: ensure
  5:   file.close
  6: end

Using yield ...

  1: def myopen(filename)
  2:   file = open(filename)
  3:   yield(file)
  4: ensure
  5:   file.close
  6: end



[ next ] [ prev ] [ contents ] Copyright 2002 by Jim Weirich.
All rights reserved.