On Tue, Jun 17, 2003 at 05:59:34AM +0900, Josef 'Jupp' Schugt wrote:
> Saluton!
> 
> * Michael Neumann; 2003-06-13, 23:05 UTC:
> > Why not make Storage a class, so you can have more than one? Your
> > current implementation does not allow this.
> 
> Done.
> 
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> 
> # EXAMPLE of using 'Storage':
> 
> require 'Storage'  # Require library
> s = Storage.new(3) # s is to hold 3 chars
> s.append('foo')    # append 'foo' to s
> s.append('bar')    # append 'bar' to s (s grows to hold text)
> s.append('baz')    # append 'baz' to s (s grows to hold text)
> puts s.to_s        # prints 'foobarbaz'
> s.resize(6)        # shrink s to hold 6 chars
> puts s.to_s        # prints 'foobar'
> s.flush            # flushes content of s
> puts s.to_s.length # 0
> puts s.capacity    # 6
> 
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

[some 86 lines of code]

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

This is a lot of work. All you need to do is expose rb_str_buf_new to
get the capa thing, matz did already the work for you :-) 
I am too tired to check if rb_str_append takes into account the capa or
only the length (but it should as the capa can be bigger than the size,
otherwise it is meaningless). If it doesn't, you need to redefine #<<
(as a singleton method of the returned string), otherwise (as I believe)
you can just stay w/ #<<.

(non tested)

#include <ruby.h>

static VALUE new(VALUE class, VALUE size) 
{
   int n = NUM2ULONG(size);

   return rb_str_buf_new(n);
}

void Init_Storage() 
{
 rb_define_singleton_method(rb_cString, "new_with_capa", new, 1);
}


and then in Ruby

buf = String.new_with_capa(1000)
buf << somedata
...etc...

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

/*
 *     Please skip to the bottom of this file if you ate lunch recently
 *                             -- Alan
 */
	-- from Linux kernel pre-2.1.91-1