Project

General

Profile

Actions

Bug #15027

closed

When Struct#each method is overriden Struct#select and Struct#to_a use wrong collections

Added by bruno (Bruno Sutic) about 6 years ago. Updated about 3 years ago.

Status:
Rejected
Target version:
-
[ruby-core:88645]
Tags:

Description

Bug

Here's the code snippet that should reproduce the problem:

class Foo < Struct.new(:bar)
  def each(&block)
    [:baz, :qux].each(&block)
  end
end

foo = Foo.new(:foo)
foo.map(&:itself) # => [:baz, :qux]  # OK

foo.to_a # => [:foo]  # NOT OK, expected [:baz, :qux]
foo.select(&:itself) # => [:foo]  # NOT OK, expected [:baz, :qux]

As you can see, even tho we defined another collection for use by overriding #each, the to_a and select still use Struct's original collection.

The problem seem to be with Struct#to_a and Struct#select methods from struct.c file that are defined unnecessarily.

Proposed solution

The attached solution simply deletes Struct#select and Struct#to_a. A couple tests are added to show everything still works as before.

Please let me know if I can provide any more info and I'll be ready to do so.


Files

struct_enumerable_fix.patch (2.8 KB) struct_enumerable_fix.patch bruno (Bruno Sutic), 08/25/2018 05:29 PM
struct_enumerable_fix2.patch (1.51 KB) struct_enumerable_fix2.patch Updated patch bruno (Bruno Sutic), 09/01/2018 04:04 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0