Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Loading a file in a buffer...

Reply
Thread Tools

Loading a file in a buffer...

 
 
Zangief Ief
Guest
Posts: n/a
 
      03-03-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      03-03-2008
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


 
Reply With Quote
 
 
 
 
Michael Steinfeld
Guest
Posts: n/a
 
      03-03-2008
On Mon, Mar 3, 2008 at 12:20 PM, Zangief Ief <(E-Mail Removed)> 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-forum.com/.
>
>


 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      03-03-2008
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

 
Reply With Quote
 
Zangief Ief
Guest
Posts: n/a
 
      03-03-2008
Thanks! 'read' method works very well
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
order of iframe loading with document loading ofir Javascript 0 12-03-2007 12:06 PM
loading image -> detect when image is done loading edfialk Javascript 0 05-10-2007 07:28 PM
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM
help needed loading a jar file and dll file Barry Java 0 09-10-2004 03:20 PM



Advertisments