Forum: Ruby output buffering

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
04f4e8457b965e5e03cc44c9cba5fe51?d=identicon&s=25 mark (Guest)
on 2006-01-02 21:43
(Received via mailing list)
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
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 Alex Fenton (Guest)
on 2006-01-03 02:38
(Received via mailing list)
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 = StringIO.new()

# redirect STDOUT
$stdout = tmp_out

puts "this and that" # would normally go to STDOUT

# restore default stdout
$stdout = 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
04f4e8457b965e5e03cc44c9cba5fe51?d=identicon&s=25 mark (Guest)
on 2006-01-03 13:20
(Received via mailing list)
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
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 Alex Fenton (Guest)
on 2006-01-03 15:49
(Received via mailing list)
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
3abc449148446ebed56b842ec4871265?d=identicon&s=25 eduard (Guest)
on 2006-01-19 18:48
(Received via mailing list)
Mark,

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

thanks,
eduard
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2006-01-20 05:56
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
3abc449148446ebed56b842ec4871265?d=identicon&s=25 eduard (Guest)
on 2006-01-20 21:13
(Received via mailing list)
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'] =
controller.response.body.length

}
50fc27333be8ff79e72a9ceba3dcb819?d=identicon&s=25 Paul Macek (pmacek)
on 2010-09-24 12:46
This is an old thread but it helped me. See my write-up here:
http://pmacek.blogspot.com/2010/09/output-bufferin...
This topic is locked and can not be replied to.