instance method
Set#classify
[X] () { (A) -> X } -> ::Hash[X, self]
classify { |o| ... } → hash
classify → enumerator
set.c
View on GitHub ↗
static VALUE
set_i_classify(VALUE set)
{
RETURN_SIZED_ENUMERATOR(set, 0, 0, set_enum_size);
VALUE args[2];
args[0] = rb_hash_new();
args[1] = rb_obj_class(set);
set_iter(set, set_classify_i, (st_data_t)args);
return args[0];
}
Classifies the set by the return value of the given block and returns a hash of {value => set of elements} pairs. The block is called once for each element of the set, passing the element as parameter.
Example
files = Set.new(Dir.glob("*.rb")) hash = files.classify { |f| File.mtime(f).year } hash #=> {2000 => Set["a.rb", "b.rb"], # 2001 => Set["c.rb", "d.rb", "e.rb"], # 2002 => Set["f.rb"]}
Returns an enumerator if no block is given.