Project

General

Profile

Bug #10984 ยป Hash#contain_.patch

olivierlacan (Olivier Lacan), 03/19/2015 01:54 PM

View differences:

w/hash.c
2402 2402
    return ary;
2403 2403
}
2404 2404

  
2405
static int
2406
hash_contain_i(VALUE key, VALUE value, VALUE arg)
2407
{
2408
    VALUE *args = (VALUE *)arg;
2409
    VALUE v = rb_hash_lookup2(args[0], key, Qundef);
2410
    if (v != Qundef && rb_equal(value, v)) return ST_CONTINUE;
2411
    args[1] = Qfalse;
2412
    return ST_STOP;
2413
}
2414

  
2415
static VALUE
2416
rb_hash_contain_p(VALUE hash, VALUE other)
2417
{
2418
    VALUE args[2];
2419

  
2420
    other = to_hash(other);
2421
    args[0] = hash;
2422
    args[1] = Qtrue;
2423
    rb_hash_foreach(other, hash_contain_i, (VALUE)args);
2424
    return args[1];
2425
}
2426

  
2405 2427
static VALUE rb_hash_compare_by_id_p(VALUE hash);
2406 2428

  
2407 2429
/*
......
3626 3648
    return env;
3627 3649
}
3628 3650

  
3651
static int
3652
env_contain_i(VALUE key, VALUE value, VALUE arg)
3653
{
3654
    VALUE *args = (VALUE *)arg;
3655
    VALUE v = rb_f_getenv(Qnil, key);
3656
    if (!NIL_P(v) && rb_equal(value, v)) return ST_CONTINUE;
3657
    args[0] = Qfalse;
3658
    return ST_STOP;
3659
}
3660

  
3661
static VALUE
3662
env_contain_p(VALUE obj, VALUE other)
3663
{
3664
    VALUE args[1];
3665

  
3666
    other = to_hash(other);
3667
    args[0] = Qtrue;
3668
    rb_hash_foreach(other, env_contain_i, (VALUE)args);
3669
    return args[0];
3670
}
3671

  
3629 3672
/*
3630 3673
 *  A Hash is a dictionary-like collection of unique keys and their values.
3631 3674
 *  Also called associative arrays, they are similar to Arrays, but where an
......
3817 3860
    rb_define_method(rb_cHash,"has_value?", rb_hash_has_value, 1);
3818 3861
    rb_define_method(rb_cHash,"key?", rb_hash_has_key, 1);
3819 3862
    rb_define_method(rb_cHash,"value?", rb_hash_has_value, 1);
3863
    rb_define_method(rb_cHash,"contain?", rb_hash_contain_p, 1);
3820 3864

  
3821 3865
    rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
3822 3866
    rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
......
3876 3920
    rb_define_singleton_method(envtbl,"to_h", env_to_hash, 0);
3877 3921
    rb_define_singleton_method(envtbl,"assoc", env_assoc, 1);
3878 3922
    rb_define_singleton_method(envtbl,"rassoc", env_rassoc, 1);
3923
    rb_define_singleton_method(envtbl,"contain?", env_contain_p, 1);
3879 3924

  
3880 3925
    /*
3881 3926
     * ENV is a Hash-like accessor for environment variables.