# String Buffering Issue

**URL:** <https://www.ruby-forum.com/t/string-buffering-issue/171276>\
**Category:** Ruby\
**Created:** [June 29, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/string-buffering-issue/171276 "2009-06-29T22:39:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [June 29, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/string-buffering-issue/171276/1 "2009-06-29T22:39:53Z")

</div>

This is an example snippet from a library im working on  
[example.rb · GitHub](http://gist.github.com/137785) I need to be able to ‘buffer’ or collect  
the results without yielding an object to any number of nested blocks.

I know this can be done with method\_missing etc but my brain hurts haha  
and for some stupid reason im stuck with this issue and have the  
incorrect output below

Output:

- 
- 
- 
- 
- 
- 
-

---

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [June 29, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/string-buffering-issue/171276/2 "2009-06-29T23:15:52Z")

</div>

On 29.06.2009 22:39, Tj Holowaychuk wrote:

> - 
> - 
> -

It seems you are trying to solve not a buffering problem but rather want  
some kind of context present for every tag you generate. You can do so  
by having the context in an object.

class Context  
def initialize  
@out = “”  
end

def tag(name, &b)  
if b  
@out \<\< “\<” \<\< name \<\< “\>”  
instance\_eval(&b)  
@out \<\< “\</” \<\< name \<\< “\>”  
else  
@out \<\< “\<” \<\< name \<\< “/\>”  
end  
end

def text(s)  
@out \<\< s  
end  
end

def markup(&b)  
c = Context.new  
c.instance\_eval(&b)  
c  
end

mup = markup do  
tag “foo” do  
tag “bar” do  
text “blah”  
end

```
 tag "empty"

```

end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [June 30, 2009, 12:01am UTC](https://www.ruby-forum.com/t/string-buffering-issue/171276/3 "2009-06-30T00:01:06Z")

</div>

Thanks for the reply!

Buffer is a bad choice of wording on my part (lack of a better name for  
the module).

The library is [http://github.com/visionmedia/tagz/tree/master](http://github.com/visionmedia/tagz/tree/master) which is  
used by [http://github.com/visionmedia/tagz/tree/master](http://github.com/visionmedia/tagz/tree/master)

Tagz is the one with the Tagz::Buffer module with this issue. The code  
is pretty much the same as what I posted above. Everything works fine  
until a method uses #tag to create nested tags, for example in Formz the  
#fieldset method returns fieldset markup and a legend in the markup as  
well so two calls to #tag in my current version start to duplicate  
markup

Hope that sort of makes sense :s
