String Buffering Issue

This is an example snippet from a library im working on
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:
<ul ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
</li>
</ul>

···

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

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

···

On 29.06.2009 22:39, Tj Holowaychuk wrote:

This is an example snippet from a library im working on
example.rb · GitHub 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:
<ul ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
</li>
</ul>

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 which is
used by 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

···

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