Best way to slurp a file into a string?

Discussion in 'Ruby' started by Wes Gamble, Mar 22, 2006.

  1. Wes Gamble

    Wes Gamble Guest

    All,

    I would like to capture the entire contents of a file in to a string (a
    la Perl) - is this the best way to do it?

    File.open(@document.path, r) do |f|
    @lines = f.readlines
    html = @lines.join
    end

    so that the html variable has all of the contents of f.

    Is there a better/easier way?

    Thanks,
    Wes

    --
    Posted via http://www.ruby-forum.com/.
    Wes Gamble, Mar 22, 2006
    #1
    1. Advertising

  2. Wes Gamble wrote:
    > All,
    >
    > I would like to capture the entire contents of a file in to a string (a
    > la Perl) - is this the best way to do it?
    >
    > File.open(@document.path, r) do |f|
    > @lines = f.readlines
    > html = @lines.join
    > end
    >
    > so that the html variable has all of the contents of f.
    >
    > Is there a better/easier way?
    >
    > Thanks,
    > Wes
    >


    IO.read(@document.path)

    Regards,

    Dan
    Daniel Berger, Mar 22, 2006
    #2
    1. Advertising

  3. Wes Gamble

    Wes Gamble Guest

    Isn't it better to do the block thing so that the file gets closed,
    etc.?

    Daniel Berger wrote:
    > Wes Gamble wrote:
    >> so that the html variable has all of the contents of f.
    >>
    >> Is there a better/easier way?
    >>
    >> Thanks,
    >> Wes
    >>

    >
    > IO.read(@document.path)
    >
    > Regards,
    >
    > Dan



    --
    Posted via http://www.ruby-forum.com/.
    Wes Gamble, Mar 22, 2006
    #3
  4. Wes Gamble

    Wes Gamble Guest

    I see that IO.read takes care of that.

    Sorry for the dumb question.

    I knew that this would be as easy (or easier) than in Perl!!!

    Wes

    Wes Gamble wrote:
    > Isn't it better to do the block thing so that the file gets closed,
    > etc.?
    >
    > Daniel Berger wrote:
    >> Wes Gamble wrote:
    >>> so that the html variable has all of the contents of f.
    >>>
    >>> Is there a better/easier way?
    >>>
    >>> Thanks,
    >>> Wes
    >>>

    >>
    >> IO.read(@document.path)
    >>
    >> Regards,
    >>
    >> Dan



    --
    Posted via http://www.ruby-forum.com/.
    Wes Gamble, Mar 22, 2006
    #4
  5. Wes Gamble wrote:
    > Isn't it better to do the block thing so that the file gets closed,
    > etc.?


    Nope. No need to worry about that with IO.read. It's not returning an open
    handle, just a string.

    Regards,

    Dan

    >
    > Daniel Berger wrote:
    >
    >>Wes Gamble wrote:
    >>
    >>>so that the html variable has all of the contents of f.
    >>>
    >>>Is there a better/easier way?
    >>>
    >>>Thanks,
    >>>Wes
    >>>

    >>
    >>IO.read(@document.path)
    >>
    >>Regards,
    >>
    >>Dan

    >
    >
    >
    Daniel Berger, Mar 22, 2006
    #5
  6. Wes Gamble

    Guest

    Personally File.read makes more sense to me than IO.read... But i've
    used both.
    , Mar 23, 2006
    #6
  7. FXRuby, FXList with (sunken) Frame

    Hello,

    how can you make a FXLists' frame sunken?
    It inherits from FXWindow which has a flag named FRAME_SUNKEN.
    But in FXList it has no effect.
    I even made a sunken container and put the list in, but that looks horrible!

    I appreciate your help.

    PS: this I tried:

    @leftlist = FXList.new(@secondhframe, nil, 0, LIST_NORMAL |
    FRAME_SUNKEN | LAYOUT_FILL_X | LAYOUT_FILL_Y)

    @leftlist = FXList.new(@secondhframe, nil, 0, FRAME_SUNKEN |
    LAYOUT_FILL_X | LAYOUT_FILL_Y)

    @leftlist = FXList.new(@secondhframe, nil, 0, FRAME_SUNKEN)
    Robert Retzbach, Mar 23, 2006
    #7
  8. Wes Gamble

    Lyle Johnson Guest

    Re: FXRuby, FXList with (sunken) Frame

    On 3/23/06, Robert Retzbach <> wrote:

    > how can you make a FXLists' frame sunken?
    > It inherits from FXWindow which has a flag named FRAME_SUNKEN.
    > But in FXList it has no effect.
    > I even made a sunken container and put the list in, but that looks horrib=

    le!

    Like Yura said, you need to put FXList inside some other container
    that is derived from FXFrame. And also, per Yura's code, be sure to
    specify zero padding around all four sides of the frame. If you use
    the default padding (which IIRC is two pixels on each side) it
    probably won't look too nice.
    Lyle Johnson, Mar 23, 2006
    #8
    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. Dick Davies
    Replies:
    1
    Views:
    104
    Gavin Sinclair
    Sep 29, 2005
  2. Helge Elvik
    Replies:
    4
    Views:
    101
  3. Charles R. Thompson
    Replies:
    6
    Views:
    138
    Ben Liddicott
    Jan 13, 2004
  4. Replies:
    7
    Views:
    214
    Big and Blue
    Dec 30, 2005
  5. kj
    Replies:
    7
    Views:
    160
Loading...

Share This Page