Ejemplo n.º 1
0
static mrb_value
mrb_ary_aset(mrb_state *mrb, mrb_value self)
{
  mrb_value v1, v2, v3;
  mrb_int i, len;

  if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) {
    switch (mrb_type(v1)) {
    /* a[n..m] = v */
    case MRB_TT_RANGE:
      if (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self))) {
        mrb_ary_splice(mrb, self, i, len, v2);
      }
      break;
    /* a[n] = v */
    case MRB_TT_FIXNUM:
      mrb_ary_set(mrb, self, mrb_fixnum(v1), v2);
      break;
    default:
      mrb_ary_set(mrb, self, aget_index(mrb, v1), v2);
      break;
    }
    return v2;
  }

  /* a[n,m] = v */
  mrb_ary_splice(mrb, self, aget_index(mrb, v1), aget_index(mrb, v2), v3);
  return v3;
}
Ejemplo n.º 2
0
static mrb_value
mrb_ary_aset(mrb_state *mrb, mrb_value self)
{
  mrb_value v1, v2, v3;
  mrb_int i, len;

  mrb_ary_modify(mrb, mrb_ary_ptr(self));
  if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) {
    /* a[n..m] = v */
    switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) {
    case 0:                   /* not range */
      mrb_ary_set(mrb, self, aget_index(mrb, v1), v2);
      break;
    case 1:                   /* range */
      mrb_ary_splice(mrb, self, i, len, v2);
      break;
    case 2:                   /* out of range */
      mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", v1);
      break;
    }
    return v2;
  }

  /* a[n,m] = v */
  mrb_ary_splice(mrb, self, aget_index(mrb, v1), aget_index(mrb, v2), v3);
  return v3;
}
Ejemplo n.º 3
0
mrb_value
mrb_ary_aset(mrb_state *mrb, mrb_value self)
{
  mrb_value *argv;
  int argc;

  mrb_get_args(mrb, "*", &argv, &argc);
  switch(argc) {
  case 2:
    if (FIXNUM_P(argv[0])) {
      mrb_ary_set(mrb, self, mrb_fixnum(argv[0]), argv[1]);
    }
    else {
      /* Should we support Range object for 1st arg ? */
      mrb_raise(mrb, E_TYPE_ERROR, "expected Fixnum for 1st argument");
    }
    break;

  case 3:
    mrb_ary_splice(mrb, self, mrb_fixnum(argv[0]), mrb_fixnum(argv[1]), argv[2]);
    break;

  default:
    mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
  }

  return self;
}