[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

output buffering

mark

1/2/2006 8:42:00 PM

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

7 Answers

Alex Fenton

1/3/2006 1:34:00 AM

0

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

mark

1/3/2006 12:16:00 PM

0

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

Alex Fenton

1/3/2006 2:47:00 PM

0

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

eduard

1/19/2006 5:10:00 PM

0

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 <alex@deleteme.pressure.to> 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 = 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
>
>

Eero Saynatkari

1/20/2006 4:20:00 AM

0

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-....


eduard

1/20/2006 7:34:00 PM

0

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

}

On 1/19/06, Eero Saynatkari <ruby-forum-reg@mailinator.com> 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-....
>
>

patsplat

1/20/2006 10:08:00 PM

0

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-o...

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/con...

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

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

Cheers,

Patrick