Regurgitating the contents of a file

P

Philip Mak

What's the shortest way to regurgitate the contents of a file? I mean
like this:

# Perl code
open(FILE, "filename.txt");
print while <FILE>;
close(FILE);

Can I do something like:

stdout << File.new("filename.txt");

or something?
 
D

Daniel Carrera

What's the shortest way to regurgitate the contents of a file? I mean
like this:

# Perl code
open(FILE, "filename.txt");
print while <FILE>;
close(FILE);

Can I do something like:

stdout << File.new("filename.txt");

or something?

puts File.open("filename.txt").to_a


Cheers,
 
R

Robert Klemme

Joel VanderWerf said:
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
 
G

Ged

Philip Mak said:
What's the shortest way to regurgitate the contents of a file? I mean
like this:

# Perl code
open(FILE, "filename.txt");
print while <FILE>;
close(FILE);

Can I do something like:

stdout << File.new("filename.txt");

or something?

IO.foreach("filename.txt") {|x| print x }
 
P

Phil Tomson

Philip Mak said:
What's the shortest way to regurgitate the contents of a file? I mean
like this:

# Perl code
open(FILE, "filename.txt");
print while <FILE>;
close(FILE);

Can I do something like:

stdout << File.new("filename.txt");

or something?

File.foreach("filename.txt") {|line| puts line }

Or if you prefer:

File.foreach("filename.txt") {|line| $stdout << line }

Phil
 
J

Josef 'Jupp' SCHUGT

Hi!

* Joel VanderWerf:
puts File.read("filename.txt")

AFAIK that reads the whole file to memory while

File.open("filename.txt") {|f| f.each {|l| puts l}}

reads one line at a time. One should add that this only works because
puts does not add a line break to strings that already end in one. It
would be better to send the String object a chomp message:

File.open("filename.txt") {|f| f.each {|l| puts l.chomp}}

Nevertheless that only plays a role when doing something more
complicated with l.

Josef 'Jupp' SCHUGT
 
A

Ara.T.Howard

Date: Tue, 27 Jan 2004 04:38:03 +0900
From: Joel VanderWerf <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: Regurgitating the contents of a file


It certainly does. But the OP did say "shortest" ;-)

irb(main):001:0> 'puts File.read("filename.txt")'.size
=> 30

irb(main):002:0> '`cat filename.txt`'.size
=> 20

;-)

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top