From: Charles Oliver Nutter Date: 2008-12-20T07:28:04+09:00 Subject: [ruby-core:20709] Re: [Feature #905] Add String.new(fixnum) to preallocate large buffer Dave Thomas wrote: > > On Dec 19, 2008, at 12:48 PM, Charles Oliver Nutter wrote: > >> Neither of those methods are polymorphic on anything. They're both new >> methods that accept a Fixnum. > > .new is And already has multiple forms in Array, so there's precedent. Also, adding multiple forms with different named arguments doesn't reduce the complexity of that single method any. > String.buffer is not a meaningful name for a constructor, in my opinion, > whereas String.new has a pedigree. By adding a initial_size: n optional > argument, you exactly express the meaning���you're asking for an initial > allocation when String.new executes. Similarly, > > Array.new([1,2,3], initial_size: 100) But Array.new(initial_size: 100).size would == 0. That's confusing...I think buffer better expresses that it's the backing store being sized than the outward expression of the String or Array itself, which is what initial_size means. I would also expect that the cost of allocating and populating an arguments hash for this would negate some of the gain from adding the new form. Array.buffer(100) adds almost no overhead on top of the physical creation of the backing store and object to wrap it, where Array.buffer(initial_size: 100) creates both a new Array and a new Hash. An implementation detail, sure, but we I think we just need something simple here. Perhaps buffer just doesn't express it clearly enough? > Right now, we have File.open("fred", "w"), rather than > File.open_write("fred"). It seems like a good idea, particularly for an > interface that's likely to grow over time (I can forsee > > String.new(initial_size: 1000, fill_with: " ", encoding: binary, etc: > ...) It seems to me this is making the semantics of String.new much more complicated, rather than simpler and more uniform. And at least encoding is already available outside of "new", so this is little more than a shortcut. But there's absolutely no way at present to allocate a string with a guaranteed backing store size, and that's the sole intention of this RFE.