Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns a new Array containing the values associated with the given keys *keys:
h = {foo: 0, bar: 1, baz: 2}
h.fetch_values(:baz, :foo) # => [2, 0]
Returns a new empty Array if no arguments given.
When a block is given, calls the block with each missing key, treating the block's return value as the value for that key:
h = {foo: 0, bar: 1, baz: 2}
values = h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
values # => [1, 0, "bad", "bam"]
When no block is given, raises an exception if any given key is not found.
 
               static VALUE
rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash)
{
    VALUE result = rb_ary_new2(argc);
    long i;

    for (i=0; i<argc; i++) {
        rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
    }
    return result;
}
            

Was this page useful?