[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Loading a file in a buffer...

Zangief Ief

3/3/2008 5:20:00 PM

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'
--
Posted via http://www.ruby-....

4 Answers

Stefano Crocco

3/3/2008 5:31:00 PM

0

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


Michael Steinfeld

3/3/2008 5:32:00 PM

0

On Mon, Mar 3, 2008 at 12:20 PM, Zangief Ief <z4n9ief@gmail.com> wrote:
> 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
>
> If you know a way to do so, please help me :)
> Thanks.
>
> Zang'
> --
> Posted via http://www.ruby-....
>
>

Morton Goldberg

3/3/2008 5:38:00 PM

0

On Mar 3, 2008, at 12:20 PM, Zangief Ief wrote:

> 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

Zangief Ief

3/3/2008 5:53:00 PM

0

Thanks! 'read' method works very well :)
--
Posted via http://www.ruby-....