output buffering

Discussion in 'Ruby' started by mark, Jan 2, 2006.

  1. mark

    mark Guest

    Hi,
    Is there any way to do php-style output buffering with ruby or rails?
    as in:

    ob_start()
    %>
    html goes in here
    <%
    html=ob_get_contents()
    ob_end_clean()
    puts html

    ----------
    Thanks,
    - Mark
     
    mark, Jan 2, 2006
    #1
    1. Advertising

  2. mark

    mark Guest

    Thanks for the reply Alex but this doesn't seem to work, maybe it's a
    mod_ruby thing but nothing after
    $stdout = tmp_out
    gets sent no matter what I do with $stdout
     
    mark, Jan 3, 2006
    #2
    1. Advertising

  3. mark

    Alex Fenton Guest

    mark wrote:
    > Thanks for the reply Alex but this doesn't seem to work, maybe it's a
    > mod_ruby thing but nothing after
    > $stdout = tmp_out
    > gets sent no matter what I do with $stdout


    I think mod_ruby overloads some of ruby's standard IO mechanisms, eg the Kernel#puts method. You probably need to restore 'standard' STDOUT in a slightly different way in mod_ruby. There's a mod_ruby mailing list where you might get a quicker answer, or try googling STDOUT + mod_ruby etc

    cheers
    alex
     
    Alex Fenton, Jan 3, 2006
    #3
  4. mark

    eduard Guest

    ------=_Part_837_9840112.1137690613804
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Mark,

    I am having a similar problem. Have you ever figured out how to do
    ob_start() in Ruby?

    thanks,
    eduard

    On 1/2/06, Alex Fenton <> wrote:
    >
    > hi
    >
    > mark wrote:
    > > Is there any way to do php-style output buffering with ruby or rails?
    > > as in:
    > > ob_start()

    >
    > In ruby, generally, you'd achieve the same thing by redirecting $stdout.
    > Something like:
    >
    > require 'stringio'
    > tmp_out =3D StringIO.new()
    >
    > # redirect STDOUT
    > $stdout =3D tmp_out
    >
    > puts "this and that" # would normally go to STDOUT
    >
    > # restore default stdout
    > $stdout =3D STDOUT
    >
    > tmp_out.rewind()
    > puts tmp_out.read() # print out what was sent to STDOUT
    >
    > I don't know whether Ruby on Rails has any special facility for
    > temporarily
    > redirecting output of literal HTML snippets within a mixed Ruby-HTML
    > template (as the PHP function does, iirc). The rails mailing list should =

    be
    > able to help you with that one.
    >
    > cheers
    > alex
    >
    >


    ------=_Part_837_9840112.1137690613804--
     
    eduard, Jan 19, 2006
    #4
  5. eduard wrote:
    > Mark,
    >
    > I am having a similar problem. Have you ever figured out how to do
    > ob_start() in Ruby?


    I am not familiar with PHP, would you be able to give an
    example where this would be useful or what this is commonly
    used for? This might enable finding a ruby equivalent.

    > thanks,
    > eduard



    E

    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Jan 20, 2006
    #5
  6. mark

    eduard Guest

    ------=_Part_6192_5946798.1137785627973
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    I was just trying to find an easy way set headers for each page.

    Tom Fakes suggested this and it works great.

    after_filter { |controller|

    controller.response.headers['Content-Length'] =3D
    controller.response.body.length

    }

    On 1/19/06, Eero Saynatkari <> wrote:
    >
    > eduard wrote:
    > > Mark,
    > >
    > > I am having a similar problem. Have you ever figured out how to do
    > > ob_start() in Ruby?

    >
    > I am not familiar with PHP, would you be able to give an
    > example where this would be useful or what this is commonly
    > used for? This might enable finding a ruby equivalent.
    >
    > > thanks,
    > > eduard

    >
    >
    > E
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    ------=_Part_6192_5946798.1137785627973--
     
    eduard, Jan 20, 2006
    #6
  7. mark

    Guest

    Hi Mark,

    mark wrote:
    > Hi,
    > Is there any way to do php-style output buffering with ruby or rails?


    ruby-web, a replacement for the ruby web libraries, offers this
    functionality:

    http://www.ruby-web.org/manual/reference-output.html

    I haven't finished testing a rails handler for ruby-web. These methods
    are implemented on a Web::Connection class with tests:


    http://svn.ruby-web.org/svn/patsplat/ruby-web/trunk/lib/web/connection.rb

    http://svn.ruby-web.org/svn/patsplat/ruby-web/trunk/test/web/test.buffer.rb

    You might be able to extract this functionality for use with rails /
    etc.

    Cheers,

    Patrick
     
    , Jan 20, 2006
    #7
  8. mark

    Paul Macek Guest

    Paul Macek, Sep 24, 2010
    #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. Tim Mohler
    Replies:
    1
    Views:
    462
    Steve Grazzini
    Sep 16, 2003
  2. =?Utf-8?B?U2NvdHRPbmVz?=

    Buffering the output of multiple controls

    =?Utf-8?B?U2NvdHRPbmVz?=, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    525
    =?Utf-8?B?a2Fub25lcw==?=
    Oct 28, 2004
  3. Mark Riordan
    Replies:
    6
    Views:
    4,949
    Mark Riordan
    Oct 15, 2004
  4. JD

    output buffering

    JD, Nov 11, 2005, in forum: Python
    Replies:
    5
    Views:
    312
  5. Shahid Juma

    Output Buffering

    Shahid Juma, Oct 5, 2004, in forum: ASP General
    Replies:
    1
    Views:
    154
    Ray Costanzo [MVP]
    Oct 5, 2004
Loading...

Share This Page