"Joel VanderWerf" <vjoel / PATH.Berkeley.EDU> schrieb im Newsbeitrag
news:40145966.9020700 / path.berkeley.edu...
> Philip Mak wrote:
> > What's the shortest way to regurgitate the contents of a file? I mean
> > like this:
>
> puts File.read("filename.txt")

This is problematic for large files since it reads the complete file into
one string before writing it again.  These idioms are more like the perl
variant, since they read only one line at a time:

File.open("filename.txt") do |io|
  line = nil
  print line while ( line = io.gets )
end

File.open("filename.txt") do |io|
  while ( line = io.gets )
    print line
  end
end

File.open("filename.txt") do |io|
  while ( line = io.gets ); print line; end
end

File.open("filename.txt") do |io|
  io.each {|line| print line}
end

on one line:
File.open("filename.txt") {|io| io.each {|line| print line} }

Regards

    robert