Project

General

Profile

Feature #8478 ยป group_by_default_empty_array.diff

phiggins (Pete Higgins), 06/03/2013 04:37 AM

View differences:

enum.c
712 712
static VALUE
713 713
enum_group_by(VALUE obj)
714 714
{
715
    VALUE hash;
715
    VALUE hash, default_val;
716 716

  
717 717
    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);
718 718

  
719 719
    hash = rb_hash_new();
720 720
    rb_block_call(obj, id_each, 0, 0, group_by_i, hash);
721
    default_val = rb_ary_new();
722
    rb_hash_set_default(hash, default_val);
721 723
    OBJ_INFECT(hash, obj);
722 724

  
723 725
    return hash;
hash.c
750 750
 *     h["cat"]   #=> #<Proc:0x401b3948@-:6>
751 751
 */
752 752

  
753
static VALUE
753
VALUE
754 754
rb_hash_set_default(VALUE hash, VALUE ifnone)
755 755
{
756 756
    rb_hash_modify_check(hash);
include/ruby/intern.h
466 466
VALUE rb_hash_delete_if(VALUE);
467 467
VALUE rb_hash_delete(VALUE,VALUE);
468 468
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone);
469
VALUE rb_hash_set_default(VALUE hash, VALUE ifnone);
469 470
typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value);
470 471
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func);
471 472
struct st_table *rb_hash_tbl(VALUE);
test/ruby/test_enum.rb
117 117

  
118 118
  def test_group_by
119 119
    h = { 1 => [1, 1], 2 => [2, 2], 3 => [3] }
120
    assert_equal(h, @obj.group_by {|x| x })
120
    grouped = @obj.group_by {|x| x }
121
    assert_equal(h, grouped)
122
    assert_equal([], grouped[42])
121 123
  end
122 124

  
123 125
  def test_first