Feature #8478 ยป group_by_default_empty_array.diff
| enum.c | ||
|---|---|---|
|
static VALUE
|
||
|
enum_group_by(VALUE obj)
|
||
|
{
|
||
|
VALUE hash;
|
||
|
VALUE hash, default_val;
|
||
|
RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
|
||
|
hash = rb_hash_new();
|
||
|
rb_block_call(obj, id_each, 0, 0, group_by_i, hash);
|
||
|
default_val = rb_ary_new();
|
||
|
rb_hash_set_default(hash, default_val);
|
||
|
OBJ_INFECT(hash, obj);
|
||
|
return hash;
|
||
| hash.c | ||
|---|---|---|
|
* h["cat"] #=> #<Proc:0x401b3948@-:6>
|
||
|
*/
|
||
|
static VALUE
|
||
|
VALUE
|
||
|
rb_hash_set_default(VALUE hash, VALUE ifnone)
|
||
|
{
|
||
|
rb_hash_modify_check(hash);
|
||
| include/ruby/intern.h | ||
|---|---|---|
|
VALUE rb_hash_delete_if(VALUE);
|
||
|
VALUE rb_hash_delete(VALUE,VALUE);
|
||
|
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone);
|
||
|
VALUE rb_hash_set_default(VALUE hash, VALUE ifnone);
|
||
|
typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value);
|
||
|
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func);
|
||
|
struct st_table *rb_hash_tbl(VALUE);
|
||
| test/ruby/test_enum.rb | ||
|---|---|---|
|
def test_group_by
|
||
|
h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
|
||
|
assert_equal(h, @obj.group_by {|x| x })
|
||
|
grouped = @obj.group_by {|x| x }
|
||
|
assert_equal(h, grouped)
|
||
|
assert_equal([], grouped[42])
|
||
|
end
|
||
|
def test_first
|
||