Project

General

Profile

Feature #15822 ยป hash-except.patch

timoschilling (Timo Schilling), 05/06/2019 06:26 PM

View differences:

hash.c
2402 2402
    return result;
2403 2403
}
2404 2404

  
2405
/*
2406
 *  call-seq:
2407
 *     hsh.except(*keys) -> a_hash
2408
 *
2409
 *  Returns a hash excluding the given keys and their values.
2410
 *
2411
 *     h = { a: 100, b: 200, c: 300 }
2412
 *     h.except(:a)          #=> {:b=>200, :c=>300}
2413
 *     h.except(:b, :c, :d)  #=> {:a=>100}
2414
 */
2415

  
2416
static VALUE
2417
rb_hash_except(int argc, VALUE *argv, VALUE hash)
2418
{
2419
    int i;
2420
    VALUE key, result;
2421

  
2422
    result = rb_obj_dup(hash);
2423

  
2424
    for (i = 0; i < argc; i++) {
2425
        key = argv[i];
2426
        rb_hash_delete(result, key);
2427
    }
2428

  
2429
    return result;
2430
}
2431

  
2405 2432
/*
2406 2433
 * call-seq:
2407 2434
 *   hsh.values_at(key, ...)   -> array
......
5694 5721
    return hash;
5695 5722
}
5696 5723

  
5724
/*
5725
 *  call-seq:
5726
 *     ENV.except(*keys) -> a_hash
5727
 *
5728
 *  Returns a hash except the given keys from ENV and their values.
5729
 *
5730
 *     ENV                       #=> {"LANG"="en_US.UTF-8", "TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
5731
 *     ENV.except("TERM","HOME") #=> {"LANG"="en_US.UTF-8"}
5732
 */
5733
static VALUE
5734
env_except(int argc, VALUE *argv)
5735
{
5736
    int i;
5737
    VALUE key, hash = env_to_hash();
5738

  
5739
    for (i = 0; i < argc; i++) {
5740
        key = argv[i];
5741
        rb_hash_delete(hash, key);
5742
    }
5743

  
5744
    return hash;
5745
}
5746

  
5697 5747
/*
5698 5748
 * call-seq:
5699 5749
 *   ENV.reject { |name, value| block } -> Hash
......
6000 6050
    rb_define_method(rb_cHash, "reject", rb_hash_reject, 0);
6001 6051
    rb_define_method(rb_cHash, "reject!", rb_hash_reject_bang, 0);
6002 6052
    rb_define_method(rb_cHash, "slice", rb_hash_slice, -1);
6053
    rb_define_method(rb_cHash, "except", rb_hash_except, -1);
6003 6054
    rb_define_method(rb_cHash, "clear", rb_hash_clear, 0);
6004 6055
    rb_define_method(rb_cHash, "invert", rb_hash_invert, 0);
6005 6056
    rb_define_method(rb_cHash, "update", rb_hash_update, -1);
......
6057 6108
    rb_define_singleton_method(envtbl, "delete_if", env_delete_if, 0);
6058 6109
    rb_define_singleton_method(envtbl, "keep_if", env_keep_if, 0);
6059 6110
    rb_define_singleton_method(envtbl, "slice", env_slice, -1);
6111
    rb_define_singleton_method(envtbl, "except", env_except, -1);
6060 6112
    rb_define_singleton_method(envtbl, "clear", rb_env_clear, 0);
6061 6113
    rb_define_singleton_method(envtbl, "reject", env_reject, 0);
6062 6114
    rb_define_singleton_method(envtbl, "reject!", env_reject_bang, 0);
test/ruby/test_env.rb
292 292
    assert_equal({"foo"=>"bar", "baz"=>"qux"}, ENV.slice("foo", "baz"))
293 293
  end
294 294

  
295
  def test_except
296
    ENV.clear
297
    ENV["foo"] = "bar"
298
    ENV["baz"] = "qux"
299
    ENV["bar"] = "rab"
300
    assert_equal({"bar"=>"rab", "baz"=>"qux", "foo"=>"bar"}, ENV.except())
301
    assert_equal({"bar"=>"rab", "baz"=>"qux", "foo"=>"bar"}, ENV.except(""))
302
    assert_equal({"bar"=>"rab", "baz"=>"qux", "foo"=>"bar"}, ENV.except("unknown"))
303
    assert_equal({"bar"=>"rab"}, ENV.except("foo", "baz"))
304
  end
305

  
295 306
  def test_clear
296 307
    ENV.clear
297 308
    assert_equal(0, ENV.size)
test/ruby/test_hash.rb
1053 1053
    assert_equal({}, {}.slice)
1054 1054
  end
1055 1055

  
1056
  def test_except
1057
    h = @cls[1=>2,3=>4,5=>6]
1058
    assert_equal({5=>6}, h.except(1, 3))
1059
    assert_equal({1=>2,3=>4,5=>6}, h.except(7))
1060
    assert_equal({1=>2,3=>4,5=>6}, h.except)
1061
    assert_equal({}, {}.except)
1062
  end
1063

  
1056 1064
  def test_filter
1057 1065
    assert_equal({3=>4,5=>6}, @cls[1=>2,3=>4,5=>6].filter {|k, v| k + v >= 7 })
1058 1066