Loading a file in a buffer...

Discussion in 'Ruby' started by Zangief Ief, Mar 3, 2008.

  1. Zangief Ief

    Zangief Ief Guest

    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/.
    Zangief Ief, Mar 3, 2008
    #1
    1. Advertising

  2. 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
    Stefano Crocco, Mar 3, 2008
    #2
    1. Advertising

  3. On Mon, 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>
    >


    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/.
    >
    >
    Michael Steinfeld, Mar 3, 2008
    #3
  4. 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
    Morton Goldberg, Mar 3, 2008
    #4
  5. Zangief Ief

    Zangief Ief Guest

    Zangief Ief, Mar 3, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,380
    John Harrison
    Jun 21, 2004
  2. Replies:
    2
    Views:
    601
    sergejusz
    Mar 26, 2007
  3. netpork
    Replies:
    1
    Views:
    539
    Heikki Toivonen
    Dec 6, 2008
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    409
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    270
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page