Mike Fletcher wrote:
> steve_rubytalk wrote:
> 
>>William James wrote:
>>
>>>ruby -i.bak -ne 'print if $_ !~ /foo/' stuff.txt
>>
>>Coo... that's a new one to me... very nifty.
>>
>>Unfortunately, I mislead you... I want to transform a file from within a
>>cgi script... which means I need to use standard out to generate other
>>feedback to the user.  Is a similar facility available within a ruby
>>program without executing a new ruby process?
> 
> 
> File.new( "stuff.txt" ) do | f |
>   f.each do |line|
>     print unless line =~ /foo/
>   end
> end
> 
> Or if you needed to rewrite it to a different file
> 
> File.new( "stuff.txt" ) do | in |
>   File.new( "newstuff.txt", "w" ) do |out|
>     in.each { | line | out.print line unless line =~ /foo/ }
>   end
> end


File.new doesn't take a block.  Use File.open.  Also, "in" is a keyword, 
so the above code produces a syntax error.  With fixes:

# Like grep -v.

File.open( "stuff.txt" ) do |input|
   File.open( "newstuff.txt", "w" ) do |output|
     input.each { |line| output.print line unless line =~ /foo/ }
   end
end