Get the content of a file

D

Dirk Einecke

Hi.

I've a method to read the content of a file.

def readFile(path)
value = ''
file = File.open(path, File::RDONLY)
while line = file.gets do
value += line
end
file.close
return value
end

I think the way line by line is not very efficiently.
Is there a shorter way to get the content of a file?

greetings
Dirk Einecke
 
T

ts

D> Is there a shorter way to get the content of a file?


svg% ri IO::read
--------------------------------------------------------------- IO::read
IO.read(rane, [length [, offset]] ) => string
------------------------------------------------------------------------
Opens the file, optionally seeks to the given offset, then returns
_length_ bytes (defaulting to the rest of the file). +read+ ensures
the file is closed before returning.

IO.read("testfile") #=> "This is line one\nThis is line two\
nThis is line three\nAnd so on...\n"
IO.read("testfile", 20) #=> "This is line one\nThi"
IO.read("testfile", 20, 10) #=> "ne one\nThis is line "

svg%



Guy Decoux
 
G

Gully Foyle

Dirk said:
Hi.

I've a method to read the content of a file.

def readFile(path)
value = ''
file = File.open(path, File::RDONLY)
while line = file.gets do
value += line
end
file.close
return value
end

I think the way line by line is not very efficiently.
Is there a shorter way to get the content of a file?

greetings
Dirk Einecke

Simple way is:

content = File.read(filename)

But if you should learn about blocks so the code you provided can be
replaced with something like the following (because your code doesn't
have exception handling to guarantee that file.close is called):

File.open(path, File::RDONLY){|file|
#use file.gets or whatever in here, preferably using another block
}
 
K

Kent Sibilev

I use the following idiom to read the whole file:

data = open(path) {|f| f.read}

Cheers,
Kent.
 
D

Dirk Einecke

Hi.

Well - thanks to all. Now I've several ways:

1.
value = open(path) {|f| f.read}

2.
file = File.open(path, File::RDONLY)
value = file.read
file.close

3.
value = File.readlines(path).to_s

4.
value = File.readlines(path).join

What is the fastes way (for Ruby) and what should I use?

greetings
Dirk Einecke
 
M

Mikael Brockman

Dirk Einecke said:
Hi.

Well - thanks to all. Now I've several ways:

1.
value = open(path) {|f| f.read}

2.
file = File.open(path, File::RDONLY)
value = file.read
file.close

3.
value = File.readlines(path).to_s

4.
value = File.readlines(path).join

What is the fastes way (for Ruby) and what should I use?

greetings
Dirk Einecke

In my opinion, number one is the best. The latter two only make sense
for line-based formats, and even in such cases it's easier to just read
it all in at once. Number two, I assume, is just like number one,
except you have to close it up yourself, which is annoying.

mikael
 
M

Michael Neumann

Dirk said:
Hi.

Well - thanks to all. Now I've several ways:

1.
value = open(path) {|f| f.read}

2.
file = File.open(path, File::RDONLY)
value = file.read
file.close

2b.

value = File.open(path) {|f| f.read}
3.
value = File.readlines(path).to_s

4.
value = File.readlines(path).join

What is the fastes way (for Ruby) and what should I use?

1. and 2. should be the fastest!


Regards,

Michael
 

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
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top