On 3/5/10, Nikolai Weibull <now / bitwi.se> wrote:
> I√ő getting the feeling thath the only real use case that we√◊e got
> for this so far is ERb.  Wouldn√’ it make more sense to change the way
> ERb (and similar Ň‘tring concatenators creates its result?

So, maybe erb can be modified to collect its result in an array which
is #join'd just before returning instead of a string which is
catenated to as you go along. In other words, instead of something
like this:

  def expand_template
     result=''
     fragments.each{|frag|
       .... #evaluate frag if appropriate
       result<<frag
     }
     return result
  end

it should do this:

  def expand_template
     result=[]
     fragments.each{|frag|
       .... #evaluate frag if appropriate
       result<<frag
     }
     return result.join
  end

This is a good suggestion, Nikolai. Array#join appears to have the
appropriate optimization already; in other words, it preallocates a
buffer (with rb_str_buf_new) that will be large enough for the total
length of all the fragments. However, I still don't see why this
facility which is available internally inside the interpreter cannot
be made available in rubyland as well. Why shouldn't there be a way to
call rb_str_buf_new directly from ruby code?

Note: I've never looked at erb source code; I'm just speculating about
what it might actually consist of.