From: Robert Klemme Date: 2008-12-19T17:26:10+09:00 Subject: [ruby-core:20692] Re: [Feature #905] Add String.new(fixnum) to preallocate large buffer 2008/12/19 Martin Duerst : > At 09:08 08/12/19, Brian Ford wrote: > >>Something like "ab" * 5 then becomes String.pattern("ab".size * 5, "ab"). > > I'm at a total loss to see why > String.pattern("ab".size * 5, "ab") > should be in any way better than > "ab" * 5 > but maybe that's just me. Can somebody explain? It's probably not. But if you want "ab" repeated 5 times, then "ab" * 5 is probably the ideal solution. Fixing the length in bytes is a new and different feature, i.e. your pattern is cut off. With String.pattern("bo",3) you would get "bob" or maybe "bo" but in a buffer of length 3 bytes. We could also name it "resize" with this semantics: String.resize(100, "foo") -> "foo" and buffer has 100 bytes String.resize(100) -> "" and buffer has 100 bytes Or maybe just "size". IMHO the idea was that you do not need to have a String beforehand so all solution which are instance methods receive a String as argument are probably suboptimal because they will typically be invoked like show above, i.e. with a String constructor which needs one object allocation (including GC bookkeeping overhead) and a bit of memory. A typical use case would be this: File.open "foo" do |io| buffer = String.size(1024) while io.read(1024, buffer) # or even io.read(buffer.capacity, buffer) $defout.write(buffer) end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end