Project

General

Profile

Bug #15027 ยป struct_enumerable_fix2.patch

Updated patch - bruno (Bruno Sutic), 09/01/2018 04:04 PM

View differences:

struct.c
860 860
static VALUE
861 861
rb_struct_to_a(VALUE s)
862 862
{
863
    if (!rb_method_basic_definition_p(CLASS_OF(s), idEach)) {
864
        return rb_call_super(0, 0);
865
    }
863 866
    return rb_ary_new4(RSTRUCT_LEN(s), RSTRUCT_CONST_PTR(s));
864 867
}
865 868

  
......
1077 1080

  
1078 1081
    rb_check_arity(argc, 0, 0);
1079 1082
    RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
1083
    if (!rb_method_basic_definition_p(CLASS_OF(s), idEach)) {
1084
        return rb_call_super(argc, argv);
1085
    }
1080 1086
    result = rb_ary_new();
1081 1087
    for (i = 0; i < RSTRUCT_LEN(s); i++) {
1082 1088
	if (RTEST(rb_yield(RSTRUCT_GET(s, i)))) {
test/ruby/test_struct.rb
393 393
    }
394 394
  end
395 395

  
396
  def test_enumerable_methods
397
    klass = @Struct.new(:a)
398
    x = klass.new("test")
399
    assert_equal(["test"], x.to_a)
400
    assert_equal(["test"], x.select { |member| member })
401
  end
402

  
403
  def test_enumerable_methods_with_overridden_each
404
    klass = @Struct.new(:a) do
405
      def each(&block)
406
        a.each_char(&block)
407
      end
408
    end
409
    x = klass.new("test")
410
    assert_equal(["t", "e", "s", "t",], x.to_a)
411
    assert_equal(["t", "e", "s", "t",], x.select { |member| member })
412
  end
413

  
396 414
  class TopStruct < Test::Unit::TestCase
397 415
    include TestStruct
398 416