Loading a file in a buffer...

Z

Zangief Ief

Hello everybody,

I would like to load the exact content of a file in a buffer (with the
same carriage return). To do so, I had trying this:

file = File.open('/home/my_file')

To verify if that work normally, I had try to add this for display the
content of my_file:

puts file

But my_file was not been printed, and I got: #<File:0x28d70>


If you know a way to do so, please help me :)
Thanks.

Zang'
 
S

Stefano Crocco

Alle Monday 03 March 2008, Zangief Ief ha scritto:
Hello everybody,

I would like to load the exact content of a file in a buffer (with the
same carriage return). To do so, I had trying this:

file = File.open('/home/my_file')

To verify if that work normally, I had try to add this for display the
content of my_file:

puts file

But my_file was not been printed, and I got: #<File:0x28d70>


If you know a way to do so, please help me :)
Thanks.

Zang'

To get the contents of a file, opening it is not enough. You need to
explicitly read its contents. There are many methods which allow to access the
contents of a file: read, readlines, each_line, each_byte, File.read,
File.readlines, File.foreach (the last three are class methods). They're
documented under class IO (from which File is derived). If you only need to
read the contents of the file you don't need to use File.open at all, but
simply use File.read:

contents = File.read('/home/my_file'/)

I hope this helps

Stefano
 
M

Michael Steinfeld

Hello everybody,

I would like to load the exact content of a file in a buffer (with the
same carriage return). To do so, I had trying this:

file = File.open('/home/my_file')

To verify if that work normally, I had try to add this for display the
content of my_file:

puts file

But my_file was not been printed, and I got: #<File:0x28d70>

try:

File.open("/home/my_file", "r") do |infile|
while (line = infile.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
end
 
M

Morton Goldberg

Hello everybody,

I would like to load the exact content of a file in a buffer (with the
same carriage return). To do so, I had trying this:

file = File.open('/home/my_file')

To verify if that work normally, I had try to add this for display the
content of my_file:

puts file

But my_file was not been printed, and I got: #<File:0x28d70>


If you know a way to do so, please help me :)


Try

buffer = IO.read('/home/my_file')

or

buffer = File.read('/home/my_file')

They both do the same thing because File inherits 'read' from IO.

Regards, Morton
 

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,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top