Hello --

On Sun, 11 Feb 2001, Michael Schuerig wrote:

> 
> Currently, when I want to pass on the block given to one method to
> called method, I do it like this:
> 
>   openAsText(file) do |f|
>     f.each do |l|
>       ...
>     end
>   end
> 
> def openAsText(file)
>   block = Proc.new
>   
>   filter = buildTextFilter(file)
>   
>   if filter.empty?
>     File.open(file) { |f| block.call(f) }
>   else
>     IO.popen("cat #{file} #{filter}") { |f| block.call(f) }
>   end
> end
> 
> What I'm wondering about is whether this conversion to a Proc is really
> necessary or if there's a more direct way to pass on the block.

You could pick it up parametrically:

  def openAsText(file,&block)

Your example looks like it might also lend itself to a nice 'yield'
approach:

  if filter.empty? 
    yield File.open(file)
  else
    yield IO.popen("cat....")
  end

in which case openAsText wouldn't have to know about the block at
all.  (I can't help it -- I love 'yield'!)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav