colin / invoca.com wrote:
> Isn't it sufficient to initialize the string buffer with String.new?

Yes, but I prefer to avoid String.new because the constant lookup
requires an inline cache lookup + storage entry in the iseq.

Here's their respective disassembly code:

''.freeze.dup
== disasm: #<ISeq:<compiled>@<compiled>>================================
0000 trace            1                                               (   1)
0002 opt_str_freeze   ""
0004 opt_send_without_block <callinfo!mid:dup, argc:0, ARGS_SIMPLE>, <callcache>
0007 leave


String.new
== disasm: #<ISeq:<compiled>@<compiled>>================================
0000 trace            1                                               (   1)
0002 getinlinecache   9, <is:0>
0005 getconstant      :String
0007 setinlinecache   <is:0>
0009 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>
0012 leave

But maybe String.new is slightly faster; but I normally
prefer smaller code unless something is called in a tight loop.