APIdock / Ruby
/
method

select

ruby latest stable - Class: Struct
select(*args)
public

Yields each member value from the struct to the block and returns an Array containing the member values from the struct for which the given block returns a true value (equivalent to Enumerable#select).

Lots = Struct .new (:a, :b, :c, :d, :e, :f)
l = Lots.new (11, 22, 33, 44, 55, 66)
l.select  {|v| v.even? } #=> [22, 44, 66]
static VALUE
rb_struct_select(int argc, VALUE *argv, VALUE s)
{
 VALUE result;
 long i;
 rb_check_arity(argc, 0, 0);
 RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
 result = rb_ary_new();
 for (i = 0; i < RSTRUCT_LEN(s); i++) {
 if (RTEST(rb_yield(RSTRUCT_GET(s, i)))) {
 rb_ary_push(result, RSTRUCT_GET(s, i));
 }
 }
 return result;
}

AltStyle によって変換されたページ (->オリジナル) /