Feature #8499 » patch.diff
hash.c | ||
---|---|---|
return rb_hash_delete_if(rb_obj_dup(hash));
|
||
}
|
||
struct slice_i_arg {
|
||
int argc;
|
||
VALUE *argv;
|
||
};
|
||
static int
|
||
slice_i(VALUE key, VALUE value, VALUE arg)
|
||
{
|
||
int i;
|
||
struct slice_i_arg *p = (struct slice_i_arg *)arg;
|
||
VALUE key_to_slice;
|
||
for (i = 0; i < p->argc; i++) {
|
||
key_to_slice = p->argv[i];
|
||
if (rb_equal(key, key_to_slice))
|
||
return ST_CONTINUE;
|
||
}
|
||
return ST_DELETE;
|
||
}
|
||
static VALUE
|
||
rb_hash_slice_bang(int argc, VALUE *argv, VALUE hash)
|
||
{
|
||
st_index_t n;
|
||
rb_hash_modify(hash);
|
||
n = RHASH_SIZE(hash);
|
||
if (!n) return Qnil;
|
||
if (argc) {
|
||
struct slice_i_arg arg;
|
||
arg.argc = argc;
|
||
arg.argv = argv;
|
||
rb_hash_foreach(hash, slice_i, (VALUE)&arg);
|
||
}
|
||
else {
|
||
rb_hash_clear(hash);
|
||
}
|
||
if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
|
||
return hash;
|
||
}
|
||
static VALUE
|
||
rb_hash_slice(int argc, VALUE *argv, VALUE hash)
|
||
{
|
||
int i;
|
||
VALUE key, value, result = rb_hash_new();
|
||
if (!argc || RHASH_EMPTY_P(hash)) return result;
|
||
for (i = 0; i < argc; i++) {
|
||
key = argv[i];
|
||
value = rb_hash_lookup2(hash, key, Qundef);
|
||
if (value != Qundef)
|
||
rb_hash_aset(result, key, value);
|
||
}
|
||
return result;
|
||
}
|
||
struct except_i_arg {
|
||
int argc;
|
||
VALUE *argv;
|
||
};
|
||
static int
|
||
except_i(VALUE key, VALUE value, VALUE arg)
|
||
{
|
||
int i;
|
||
struct except_i_arg *p = (struct except_i_arg *)arg;
|
||
VALUE key_to_except;
|
||
for (i = 0; i < p->argc; i++) {
|
||
key_to_except = p->argv[i];
|
||
if (rb_equal(key, key_to_except))
|
||
return ST_DELETE;
|
||
}
|
||
return ST_CONTINUE;
|
||
}
|
||
static VALUE
|
||
rb_hash_except_bang(int argc, VALUE *argv, VALUE hash)
|
||
{
|
||
st_index_t n;
|
||
struct except_i_arg arg;
|
||
rb_hash_modify(hash);
|
||
n = RHASH_SIZE(hash);
|
||
if (!n || !argc) return Qnil;
|
||
arg.argc = argc;
|
||
arg.argv = argv;
|
||
rb_hash_foreach(hash, except_i, (VALUE)&arg);
|
||
if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
|
||
return hash;
|
||
}
|
||
static VALUE
|
||
rb_hash_except(int argc, VALUE *argv, VALUE hash)
|
||
{
|
||
VALUE result = rb_obj_dup(hash);
|
||
rb_hash_except_bang(argc, argv, result);
|
||
return result;
|
||
}
|
||
/*
|
||
* call-seq:
|
||
* hsh.values_at(key, ...) -> array
|
||
... | ... | |
rb_define_method(rb_cHash,"select!", rb_hash_select_bang, 0);
|
||
rb_define_method(rb_cHash,"reject", rb_hash_reject, 0);
|
||
rb_define_method(rb_cHash,"reject!", rb_hash_reject_bang, 0);
|
||
rb_define_method(rb_cHash,"slice", rb_hash_slice, -1);
|
||
rb_define_method(rb_cHash,"slice!", rb_hash_slice_bang, -1);
|
||
rb_define_method(rb_cHash,"except", rb_hash_except, -1);
|
||
rb_define_method(rb_cHash,"except!", rb_hash_except_bang, -1);
|
||
rb_define_method(rb_cHash,"clear", rb_hash_clear, 0);
|
||
rb_define_method(rb_cHash,"invert", rb_hash_invert, 0);
|
||
rb_define_method(rb_cHash,"update", rb_hash_update, 1);
|
test/ruby/test_hash.rb | ||
---|---|---|
assert_equal(nil, h.select!{true})
|
||
end
|
||
def test_slice
|
||
h = {1=>2,3=>4,5=>6}
|
||
assert_equal({1=>2, 3=>4}, h.slice(1, 3))
|
||
assert_equal({}, h.slice)
|
||
end
|
||
def test_slice!
|
||
h = {1=>2,3=>4,5=>6}
|
||
assert_equal({1=>2, 3=>4}, h.slice!(1, 3))
|
||
assert_equal({1=>2, 3=>4}, h)
|
||
assert_equal(nil, h.slice!(1, 3))
|
||
assert_equal({}, h.slice!(7))
|
||
assert_equal(nil, h.slice!)
|
||
end
|
||
def test_except
|
||
h = {1=>2,3=>4,5=>6}
|
||
assert_equal({5=>6}, h.except(1, 3))
|
||
assert_equal(h, h.except)
|
||
end
|
||
def test_except!
|
||
h = {1=>2,3=>4,5=>6}
|
||
assert_equal({5=>6}, h.except!(1, 3))
|
||
assert_equal({5=>6}, h)
|
||
assert_equal(nil, h.except!(1,3))
|
||
assert_equal(nil, h.except!)
|
||
assert_equal({5=>6}, h)
|
||
end
|
||
def test_clear2
|
||
assert_equal({}, {1=>2,3=>4,5=>6}.clear)
|
||
h = {1=>2,3=>4,5=>6}
|