[#100309] How to use backport custom field — Jun Aruga <jaruga@...>
Please allow my ignorance.
9 messages
2020/10/06
[#100310] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
IkJhY2twb3J0IGN1c3RvbSBmaWVsZCIgaXMgb25seSBhdmFpbGFibGUgZm9yIHRpY2tldHMgd2hv
[#100311] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/06
On Tue, Oct 6, 2020 at 4:44 PM NARUSE, Yui <[email protected]> wrote:
[#100314] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
VGhhbmsgeW91IGZvciBjb25maXJtYXRpb24uCkkgY2hlY2tlZCBhZ2FpbiBhbmQgdG8gZWRpdCBi
[#100322] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Tue, Oct 6, 2020 at 7:25 PM NARUSE, Yui <[email protected]> wrote:
[#100326] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/07
SSBhZGRlZCB5b3UgdG8gIlJlcG9ydGVyIiByb2xlIGluIHRoZSBwcm9qZWN0CgoyMDIw5bm0MTDm
[#100327] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Wed, Oct 7, 2020 at 1:42 PM NARUSE, Yui <[email protected]> wrote:
[ruby-core:100511] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
From:
merch-redmine@...
Date:
2020-10-23 04:10:08 UTC
List:
ruby-core #100511
Issue #6087 has been updated by jeremyevans0 (Jeremy Evans).
matz (Yukihiro Matsumoto) wrote in #note-13:
> Should we do an experiment in 3.0?
I've added a pull request that modifies the Array methods to return Array instances instead of subclass instances: https://github.com/ruby/ruby/pull/3690
----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass?
https://bugs.ruby-lang.org/issues/6087#change-88134
* Author: marcandre (Marc-Andre Lafortune)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 3.0
* ruby -v: trunk
----------------------------------------
Just noticed that we still don't have a consistent way to handle return values:
```ruby
class A < Array
end
a = A.new
a.flatten.class # => A
a.rotate.class # => Array
(a * 2).class # => A
(a + a).class # => Array
```
Some methods are even inconsistent depending on their arguments:
```ruby
a.slice!(0, 1).class # => A
a.slice!(0..0).class # => A
a.slice!(0, 0).class # => Array
a.slice!(1, 0).class # => Array
a.slice!(1..0).class # => Array
```
Finally, there is currently no constructor nor hook called when making these new copies, so they are never properly constructed.
Imagine this simplified class that relies on `@foo` holding a hash:
```ruby
class A < Array
def initialize(*args)
super
@foo = {}
end
def initialize_copy(orig)
super
@foo = @foo.dup
end
end
a = A.new.flatten
a.class # => A
a.instance_variable_get(:@foo) # => nil, should never happen
```
I feel this violates object orientation.
One solution is to always return the base class (`Array`/`String`/...).
Another solution is to return the current subclass. To be object oriented, I feel we must do an actual `dup` of the object, including copying the instance variables, if any, and calling `initialize_copy`. Exceptions to this would be (1) explicit documentation, e.g. `Array#to_a`, or (2) methods inherited from a module (like `Enumerable` methods for `Array`).
I'll be glad to fix these once there is a decision made on which way to go.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>