select(...)
public
Returns a new array consisting of
[key,value] pairs for which the block returns true. Also see
Hash.values_at.
h = { "a" => 100, "b" => 200, "c" => 300 }
h.select {|k,v| k > "a"}
h.select {|k,v| v < 200}
Show source
/*
* call-seq:
* hsh.select {|key, value| block} => array
*
* Returns a new array consisting of <code>[key,value]</code>
* pairs for which the block returns true.
* Also see <code>Hash.values_at</code>.
*
* h = { "a" => 100, "b" => 200, "c" => 300 }
* h.select {|k,v| k > "a"} #=> [["b", 200], ["c", 300]]
* h.select {|k,v| v < 200} #=> [["a", 100]]
*/
VALUE
rb_hash_select(argc, argv, hash)
int argc;
VALUE *argv;
VALUE hash;
{
VALUE result;
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
result = rb_ary_new();
rb_hash_foreach(hash, select_i, result);
return result;
}