[ruby-core:76078] [Ruby trunk Bug#12437] Is it "legal" to call collect! in class initializer?

From: valtri@...
Date: 2016-06-19 18:54:27 UTC
List: ruby-core #76078
Issue #12437 has been updated by Frantiナ。ek Dvoナ凖。k.

File collect-workaround.rb added

Actually it could be fixed by reverting this patch:

https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52591

Or it is possible to worked around that in the "Category" class by using custom collect method with the old code.

So the question is, how it is from ruby language point of view?

The revision 52591 is announced as micro-optimization, so it could be safe to remove it...

----------------------------------------
Bug #12437: Is it "legal" to call collect! in class initializer?
https://bugs.ruby-lang.org/issues/12437#change-59280

* Author: Vit Ondruch
* Status: Assigned
* Priority: Normal
* Assignee: Akinori MUSHA
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
* Backport: 2.1: DONTNEED, 2.2: DONTNEED, 2.3: REQUIRED
----------------------------------------
Is there any reason the following script should not work?

```ruby
#! /usr/bin/ruby

require 'set'

class Categories < Set

  def initialize(categories=[])
    categories.collect! { |category| category } if categories
    super categories
  end

end

categories = Categories.new()
categories += [1, 2, 3]
p categories

categories2 = Categories.new(categories)
p categories2
```

It fails with ```stack level too deep (SystemStackError)``` error and this regression seems to be introduced by r52591.

For details, please take a look at original issue reported here: https://bugzilla.redhat.com/show_bug.cgi?id=1308057


---Files--------------------------------
collect-workaround.rb (452 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next