class ArrayBuffer

Public Class Methods

new(p1) click to toggle source
static VALUE
t_bb_initialize(VALUE self, VALUE size) {
  DECLAREBB(self);
  unsigned int s = NUM2UINT(size);
  bb->size = s;
  if (bb->backing_str)
    rb_gc_mark(bb->backing_str);

  bb->backing_str = rb_str_buf_new(s);

  t_bb_reassign_ptr(bb);
  memset(bb->ptr, 0, (size_t)s);
  return self;
}

Public Instance Methods

[](p1) click to toggle source
static VALUE
t_bb_getbyte(VALUE self, VALUE index) {
  DECLAREBB(self);
  int idx = NUM2INT(index);
  if (idx < 0)
    idx += (int)bb->size;
  CHECKBOUNDS(bb, idx);
  return UINT2NUM((unsigned int)bb->ptr[idx]);
}
[]=(p1, p2) click to toggle source
static VALUE
t_bb_setbyte(VALUE self, VALUE index, VALUE value) {
  DECLAREBB(self);
  int idx = NUM2INT(index);
  unsigned int val = NUM2UINT(value);
  if (idx < 0)
    idx += (int)bb->size;
  CHECKBOUNDS(bb, idx);
  bb->ptr[idx] = (unsigned char)val;
  return self;
}
bytes() click to toggle source

Returns a ASCII-8BIT string with the contents of the buffer

The returned string is the backing string of the buffer. It's encoding is always ASCII-8BIT. If the buffer has size zero, an empty string is returned.

@return [String]

static VALUE
t_bb_bytes(VALUE self) {
  DECLAREBB(self);
  return bb->backing_str;
}
Also aliased as: to_s
each() click to toggle source
static VALUE
t_bb_each(VALUE self) {
  DECLAREBB(self);

  if (rb_block_given_p()) {
    for (int i = 0; i < bb->size; i++) {
      unsigned int val = (unsigned int)bb->ptr[i];
      rb_yield(UINT2NUM(val));
    }
  } else {
    rb_raise(rb_eArgError, "no block given");
  }

  return self;
}
length()
Alias for: size
realloc(p1) click to toggle source
static VALUE
t_bb_realloc(VALUE self, VALUE _new_size) {
  DECLAREBB(self);
  unsigned int new_size = NUM2UINT(_new_size);
  if (new_size == bb->size)
    return self;

  rb_str_resize(bb->backing_str, new_size);
  bb->size = new_size;
  t_bb_reassign_ptr(bb);

  return self;
}
size() click to toggle source
static VALUE
t_bb_size(VALUE self) {
  DECLAREBB(self);
  return UINT2NUM(bb->size);
}
Also aliased as: length
to_s()

Returns a ASCII-8BIT string with the contents of the buffer

The returned string is the backing string of the buffer. It's encoding is always ASCII-8BIT. If the buffer has size zero, an empty string is returned.

@return [String]

Alias for: bytes