Issue #10845 has been updated by Eregon (Benoit Daloze).


jeremyevans0 (Jeremy Evans) wrote in #note-13:
> I looked at `Hash#merge`, but it doesn't have the same issue as the String and Array methods, since it is implemented as `dup.merge!`, and `dup` copies the state into the new object instead of losing it.

Indeed, I think too that case is fine.

I checked all Hash methods listed in the docs and returning a Hash and not just returning `self`: `compact, filter, select, reject, invert, transform_keys, transform_values`, and all of them return Hash, except for `merge`:

```ruby
h = SubHash.new
h[:foo] = 42

# Seems fine, uses #dup
h.merge({}).class # => SubHash
```
(`keep_if` returns `self`, that confused me)

So `Hash` is fine already :)

----------------------------------------
Bug #10845: Subclassing String
https://bugs.ruby-lang.org/issues/10845#change-88658

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* ruby -v: 2.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
If I make a subclass of `String`, the method `*` returns an instance of that class.

~~~ruby
class MyString < String
end

MyString.new("foo").*(2).class #=> MyString
~~~

This is different from other similar operations like `+` and `%`, which return a `String` instance.

~~~ruby
MyString.new("foo").+("bar").class #=> String
MyString.new("%{foo}").%(foo: "bar").class #=> String
~~~

I don't see clear reason why `*` is to be different from `+` and `%`, and thought that perhaps either the behaviour with `*` is a bug, or the behaviour with `+` and `%` is a bug.

Or, is a reason why they are different?



-- 
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>