diff --git a/hash.c b/hash.c index 7198e8a..3b74d10 100644 --- a/hash.c +++ b/hash.c @@ -1336,6 +1336,118 @@ rb_hash_reject(VALUE hash) } /* + * call-seq: + * hsh.slice! -> a_hash + * + * Replaces the hash with only the given keys. + * Returns a hash containing the removed key/value pairs. + * + * h = { "a" => 100, "b" => 200, "c" => 300 } + * h.slice!("a") #=> {"b" => 200, "c" => 300} + * h #=> {"a" => 100} + */ + +static VALUE +rb_hash_slice_bang(int argc, VALUE *argv, VALUE hash) +{ + int i; + VALUE key, value, result; + + rb_hash_modify_check(hash); + if (RHASH_EMPTY_P(hash)) { + return rb_hash_new(); + } + + result = rb_hash_dup(hash); + rb_hash_clear(hash); + + for (i = 0; i < argc; i++) { + key = argv[i]; + value = rb_hash_delete_entry(result, key); + if (value != Qundef) + rb_hash_aset(hash, key, value); + } + + return result; +} + +/* + * call-seq: + * hsh.slice -> a_hash + * + * Slices a hash to include only the given keys. + * Returns a hash containing the given keys. + * + * h = { "a" => 100, "b" => 200, "c" => 300 } + * h.slice("a") #=> {"a" => 100} + */ + +static VALUE +rb_hash_slice(int argc, VALUE *argv, VALUE hash) +{ + int i; + VALUE key, value, result; + + if (argc == 0 || RHASH_EMPTY_P(hash)) { + return rb_hash_new(); + } + result = rb_hash_new_with_size(argc); + + 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; +} + +/* + * call-seq: + * hsh.except! -> a_hash + * + * Removes the given keys from hash and returns it. + * + * h = { "a" => 100, "b" => 200, "c" => 300 } + * h.except!("a") #=> {"b" => 200, "c" => 300} + * h #=> {"b" => 200, "c" => 300} + */ + +static VALUE +rb_hash_except_bang(int argc, VALUE *argv, VALUE hash) +{ + int i; + VALUE key; + + rb_hash_modify_check(hash); + + for (i = 0; i < argc; i++) { + key = argv[i]; + rb_hash_delete_entry(hash, key); + } + + return hash; +} + +/* + * call-seq: + * hsh.except -> a_hash + * + * Returns a hash that includes everything except given keys. + * + * h = { "a" => 100, "b" => 200, "c" => 300 } + * h.except("a") #=> {"b" => 200, "c" => 300} + * h #=> { "a" => 100, "b" => 200, "c" => 300 } + */ + +static VALUE +rb_hash_except(int argc, VALUE *argv, VALUE hash) +{ + return rb_hash_except_bang(argc, argv, rb_hash_dup(hash)); +} + +/* * call-seq: * hsh.values_at(key, ...) -> array * @@ -4596,6 +4708,10 @@ Init_Hash(void) 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); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index ebc622a..46ae6ae 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1006,6 +1006,43 @@ def test_select! assert_equal(nil, h.select!{true}) end + def test_slice + h = @cls[1=>2,3=>4,5=>6] + assert_equal({1=>2, 3=>4}, h.slice(1, 3)) + assert_equal({}, h.slice(7)) + assert_equal({}, h.slice) + assert_equal({}, {}.slice) + end + + def test_slice! + h = @cls[1=>2,3=>4,5=>6] + assert_equal({5=>6}, h.slice!(1, 3)) + assert_equal({1=>2,3=>4}, h) + assert_equal({1=>2,3=>4}, h.slice!) + assert_equal({}, h) + h = @cls[1=>2,3=>4,5=>6] + assert_equal({1=>2,3=>4,5=>6}, h.slice!(7)) + assert_equal({}, h) + assert_equal({}, {}.slice!) + end + + def test_excpet + h = @cls[1=>2,3=>4,5=>6] + assert_equal({1=>2,3=>4,5=>6}, h.except(7)) + assert_equal({5=>6}, h.except(1, 3)) + assert_equal({}, {}.except) + end + + def test_except! + h = @cls[1=>2,3=>4,5=>6] + assert_equal({1=>2,3=>4,5=>6}, h.except!(7)) + assert_equal({5=>6}, h.except!(1, 3)) + assert_equal({5=>6}, h) + assert_equal({5=>6}, h.except!) + assert_equal({5=>6}, h) + assert_equal({}, {}.except!) + end + def test_clear2 assert_equal({}, @cls[1=>2,3=>4,5=>6].clear) h = @cls[1=>2,3=>4,5=>6]