Project

General

Profile

values_at_bang.patch

wojtekmach (Wojtek Mach), 07/08/2014 10:35 PM

View differences:

hash.c
1202 1202
    return result;
1203 1203
}
1204
/*
1205
 * call-seq:
1206
 *   hsh.values_at!(key, ...)                 -> array
1207
 *   hsh.values_at!(key, ...) { |key| block } -> array
1208
 *
1209
 * Return an array containing the values associated with the given keys
1210
 * but also raises an <code>KeyError</code> when one of keys can't be found.
1211
 * Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>.
1212
 *
1213
 *   h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1214
 *
1215
 *   h.values_at!("cow", "cat")                   #=> ["bovine", "feline"]
1216
 *   h.values_at!("cow", "bird")                  # raises KeyError
1217
 *   h.values_at!("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
1218
 */
1219

  
1220
VALUE
1221
rb_hash_values_at_bang(int argc, VALUE *argv, VALUE hash)
1222
{
1223
    VALUE result = rb_ary_new2(argc);
1224
    long i;
1225

  
1226
    for (i=0; i<argc; i++) {
1227
      rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
1228
    }
1229
    return result;
1230
}
1231

  
1204 1232
static int
1205 1233
select_i(VALUE key, VALUE value, VALUE result)
1206 1234
{
......
3816 3844
    rb_define_method(rb_cHash,"keys", rb_hash_keys, 0);
3817 3845
    rb_define_method(rb_cHash,"values", rb_hash_values, 0);
3818 3846
    rb_define_method(rb_cHash,"values_at", rb_hash_values_at, -1);
3847
    rb_define_method(rb_cHash,"values_at!", rb_hash_values_at_bang, -1);
3819 3848
    rb_define_method(rb_cHash,"shift", rb_hash_shift, 0);
3820 3849
    rb_define_method(rb_cHash,"delete", rb_hash_delete, 1);