Index: hash.c =================================================================== --- hash.c (revision 58506) +++ hash.c (working copy) @@ -1131,17 +1131,20 @@ /* * call-seq: - * hsh.delete(key) -> value - * hsh.delete(key) {| key | block } -> value + * hsh.delete(key) -> value + * hsh.delete(key, ...) -> array + * hsh.delete(key) {| key | block } -> value + * hsh.delete(key, ...) {| key | block } -> array * - * Deletes the key-value pair and returns the value from hsh whose - * key is equal to key. If the key is not found, it returns - * nil. If the optional code block is given and the + * Deletes one or more key-value pairs and returns the values from hsh + * whose keys were given. If any of the keys are not found, it returns + * nil for those values. If the optional code block is given and a * key is not found, pass in the key and return the result of * block. * - * h = { "a" => 100, "b" => 200 } - * h.delete("a") #=> 100 + * h = { "a" => 100, "b" => 200, "c" => 300 } + * h.delete("a", "b") #=> [100, 200] + * h.delete("c") #=> 300 * h.delete("z") #=> nil * h.delete("z") { |el| "#{el} not found" } #=> "z not found" * @@ -1148,23 +1151,33 @@ */ static VALUE -rb_hash_delete_m(VALUE hash, VALUE key) +rb_hash_delete_m(int argc, VALUE *argv, VALUE hash) { + rb_hash_modify_check(hash); + VALUE val; + VALUE result = rb_ary_new2(argc); + long i; - rb_hash_modify_check(hash); - val = rb_hash_delete_entry(hash, key); + for (i=0; i 1) { + return result; + } else { - if (rb_block_given_p()) { - return rb_yield(key); - } - else { - return Qnil; - } + return rb_ary_entry(result, 0); } } @@ -4479,7 +4492,7 @@ rb_define_method(rb_cHash, "fetch_values", rb_hash_fetch_values, -1); rb_define_method(rb_cHash, "shift", rb_hash_shift, 0); - rb_define_method(rb_cHash, "delete", rb_hash_delete_m, 1); + rb_define_method(rb_cHash, "delete", rb_hash_delete_m, -1); rb_define_method(rb_cHash, "delete_if", rb_hash_delete_if, 0); rb_define_method(rb_cHash, "keep_if", rb_hash_keep_if, 0); rb_define_method(rb_cHash, "select", rb_hash_select, 0); Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 58506) +++ test/ruby/test_hash.rb (working copy) @@ -350,11 +350,11 @@ end def test_delete - h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true' ] + h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true', false => 'false' ] h2 = @cls[ 1 => 'one', 2 => 'two' ] h3 = @cls[ 2 => 'two' ] - assert_equal('true', h1.delete(true)) + assert_equal(['true', nil, 'false'], h1.delete(true, nil, false)) assert_equal(h2, h1) assert_equal('one', h1.delete(1))