# Output buffering

**URL:** <https://www.ruby-forum.com/t/output-buffering/51543>\
**Category:** Ruby\
**Created:** [January 2, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/output-buffering/51543 "2006-01-02T21:43:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 2, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/1 "2006-01-02T21:43:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 3, 2006, 2:38am UTC](https://www.ruby-forum.com/t/output-buffering/51543/2 "2006-01-03T02:38:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 3, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/3 "2006-01-03T13:20:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 19, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/4 "2006-01-19T18:48:46Z")

</div>

Mark,

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

thanks,  
eduard

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 3, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/5 "2006-01-03T15:49:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 20, 2006, 5:56am UTC](https://www.ruby-forum.com/t/output-buffering/51543/6 "2006-01-20T05:56:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 20, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/7 "2006-01-20T21:13:05Z")

</div>

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

Tom F. suggested this and it works great.

after\_filter { |controller|

```
    controller.response.headers['Content-Length'] =

```

controller.response.body.length

}

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 24, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/output-buffering/51543/8 "2010-09-24T12:46:23Z")

</div>

This is an old thread but it helped me. See my write-up here:  
[http://pmacek.blogspot.com/2010/09/output-buffering-with-ruby.html](http://pmacek.blogspot.com/2010/09/output-buffering-with-ruby.html)
