Project

General

Profile

Feature #13211 ยป hash-delete-multiple.patch

kddeisz (Kevin Deisz), 04/29/2017 08:40 PM

View differences:

hash.c (working copy)
1131 1131

  
1132 1132
/*
1133 1133
 *  call-seq:
1134
 *     hsh.delete(key)                   -> value
1135
 *     hsh.delete(key) {| key | block }  -> value
1134
 *     hsh.delete(key)                        -> value
1135
 *     hsh.delete(key, ...)                   -> array
1136
 *     hsh.delete(key) {| key | block }       -> value
1137
 *     hsh.delete(key, ...) {| key | block }  -> array
1136 1138
 *
1137
 *  Deletes the key-value pair and returns the value from <i>hsh</i> whose
1138
 *  key is equal to <i>key</i>. If the key is not found, it returns
1139
 *  <em>nil</em>. If the optional code block is given and the
1139
 *  Deletes one or more key-value pairs and returns the values from <i>hsh</i>
1140
 *  whose keys were given. If any of the keys are not found, it returns
1141
 *  <em>nil</em> for those values. If the optional code block is given and a
1140 1142
 *  key is not found, pass in the key and return the result of
1141 1143
 *  <i>block</i>.
1142 1144
 *
1143
 *     h = { "a" => 100, "b" => 200 }
1144
 *     h.delete("a")                              #=> 100
1145
 *     h = { "a" => 100, "b" => 200, "c" => 300 }
1146
 *     h.delete("a", "b")                         #=> [100, 200]
1147
 *     h.delete("c")                              #=> 300
1145 1148
 *     h.delete("z")                              #=> nil
1146 1149
 *     h.delete("z") { |el| "#{el} not found" }   #=> "z not found"
1147 1150
 *
1148 1151
 */
1149 1152

  
1150 1153
static VALUE
1151
rb_hash_delete_m(VALUE hash, VALUE key)
1154
rb_hash_delete_m(int argc, VALUE *argv, VALUE hash)
1152 1155
{
1156
    rb_hash_modify_check(hash);
1157

  
1153 1158
    VALUE val;
1159
    VALUE result = rb_ary_new2(argc);
1160
    long i;
1154 1161

  
1155
    rb_hash_modify_check(hash);
1156
    val = rb_hash_delete_entry(hash, key);
1162
    for (i=0; i<argc; i++) {
1163
      val = rb_hash_delete_entry(hash, argv[i]);
1157 1164

  
1158
    if (val != Qundef) {
1159
	return val;
1165
      if (val != Qundef) {
1166
        rb_ary_push(result, val);
1167
      }
1168
      else if (rb_block_given_p()) {
1169
        rb_ary_push(result, rb_yield(argv[i]));
1170
      }
1171
      else {
1172
        rb_ary_push(result, Qnil);
1173
      }
1160 1174
    }
1175

  
1176
    if (argc > 1) {
1177
      return result;
1178
    }
1161 1179
    else {
1162
	if (rb_block_given_p()) {
1163
	    return rb_yield(key);
1164
	}
1165
	else {
1166
	    return Qnil;
1167
	}
1180
      return rb_ary_entry(result, 0);
1168 1181
    }
1169 1182
}
1170 1183

  
......
4479 4492
    rb_define_method(rb_cHash, "fetch_values", rb_hash_fetch_values, -1);
4480 4493

  
4481 4494
    rb_define_method(rb_cHash, "shift", rb_hash_shift, 0);
4482
    rb_define_method(rb_cHash, "delete", rb_hash_delete_m, 1);
4495
    rb_define_method(rb_cHash, "delete", rb_hash_delete_m, -1);
4483 4496
    rb_define_method(rb_cHash, "delete_if", rb_hash_delete_if, 0);
4484 4497
    rb_define_method(rb_cHash, "keep_if", rb_hash_keep_if, 0);
4485 4498
    rb_define_method(rb_cHash, "select", rb_hash_select, 0);
test/ruby/test_hash.rb (working copy)
350 350
  end
351 351

  
352 352
  def test_delete
353
    h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true' ]
353
    h1 = @cls[ 1 => 'one', 2 => 'two', true => 'true', false => 'false' ]
354 354
    h2 = @cls[ 1 => 'one', 2 => 'two' ]
355 355
    h3 = @cls[ 2 => 'two' ]
356 356

  
357
    assert_equal('true', h1.delete(true))
357
    assert_equal(['true', nil, 'false'], h1.delete(true, nil, false))
358 358
    assert_equal(h2, h1)
359 359

  
360 360
    assert_equal('one', h1.delete(1))