Skip to content

Commit 28706d7

Browse files
committed
[en,ja]Add regression-of-hash-reject-in-ruby-2-1-1
Closes #665
1 parent fc08cb5 commit 28706d7

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: news_post
3+
title: "Regression of Hash#reject in Ruby 2.1.1"
4+
author: "sorah"
5+
translator:
6+
date: 2014-03-10 14:00:00 +0000
7+
lang: en
8+
---
9+
10+
In Ruby 2.1.0 or earlier, the `reject` method in any class that inherits `Hash` returns an object of its own class.
11+
But in Ruby 2.1.1, this behavior has changed accidentally to return always a plain Hash object, not of the inherited class.
12+
13+
{% highlight ruby %}
14+
class SubHash < Hash
15+
end
16+
17+
p Hash.new.reject { }.class
18+
#=> 2.1.0: Hash, 2.1.1: Hash
19+
p SubHash.new.reject { }.class
20+
#=> 2.1.0: SubHash, 2.1.1: Hash
21+
{% endhighlight %}
22+
23+
(To be exact, extra states such as instance variables, etc aren't copied either.)
24+
25+
Ruby 2.1.1 shouldn't include such behavior changes, because with the release of Ruby 2.1.0 we've [changed our versioning policy](https://www.ruby-lang.org/en/news/2013/12/21/semantic-versioning-after-2-1-0/), so Ruby 2.1.1 is a patch level release and it shouldn't break backwards compatibility.
26+
27+
This regression could potentially affect many libraries, one such case is Rails' `HashWithIndifferentAccess` and `OrderedHash`. They are broken: https://github.com/rails/rails/issues/14188
28+
29+
This behavior will be reverted to the 2.1.0 behavior in Ruby 2.1.2, but is expected to be the default behavior for Ruby 2.2.0: [Feature #9223](https://bugs.ruby-lang.org/issues/9223).
30+
So we recommend to fix your code to in order to expect this behavior change.
31+
32+
This accident is caused by one missing backport commit. For more detail, see http://blog.sorah.jp/2014/03/10/hash-reject-regression-in-ruby211
33+
34+
Sorry for any inconvenience, and thank you for your support.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: news_post
3+
title: "Ruby 2.1.1 に含まれる Hash#reject の不具合について"
4+
author: "sorah"
5+
translator: "sorah"
6+
date: 2014-03-10 14:00:00 +0000
7+
lang: ja
8+
---
9+
10+
Ruby 2.1.0 まで、`Hash` を継承したクラスの `reject` メソッドはその継承したクラスのオブジェクトを返す挙動になっていました。
11+
しかし、Ruby 2.1.1 で意図せずその挙動が変更され、継承したクラスの場合でも必ず Hash クラスのオブジェクトが返る挙動に変更されてしまっています。
12+
13+
{% highlight ruby %}
14+
class SubHash < Hash
15+
end
16+
17+
p Hash.new.reject { }.class
18+
#=> 2.1.0: Hash, 2.1.1: Hash
19+
p SubHash.new.reject { }.class
20+
#=> 2.1.0: SubHash, 2.1.1: Hash
21+
{% endhighlight %}
22+
23+
(正確にはインスタンス変数など他の属性もコピーされなくなっています。)
24+
25+
Ruby 2.1.0 から[バージョニングポリシーを変更している](https://www.ruby-lang.org/ja/news/2013/12/21/semantic-versioning-after-2-1-0/)ため、Ruby 2.1.1 はパッチレベルリリースにあたります。パッチレベルリリースは後方互換性を維持すべきなため、本来 Ruby 2.1.1 にこのような挙動の変更が入るべきではありません。
26+
27+
この意図しない挙動変更はいくつかのライブラリに影響を与えてしまう可能性があります。一つ観測されている事例は
28+
Rails の `HashWithIndifferentAccess``OrderedHash` です: https://github.com/rails/rails/issues/14188
29+
30+
この挙動変更は Ruby 2.1.2 で元に戻るよう修正される予定です。しかし、これは [Feature #9223](https://bugs.ruby-lang.org/issues/9223) で議論されていたもので、Ruby 2.2.0 で導入される予定です。
31+
われわれはあなたのコードをこの仕様変更を想定したものに修正する事を推奨します。
32+
33+
また、この不具合はあるコミットのバックポート漏れで発生してしまいました。対策などを含めた詳細はこちらの記事を参照してください: http://diary.sorah.jp/2014/02/28/ruby211-hash-reject
34+
35+
ご不便をおかけします。
36+

0 commit comments

Comments
 (0)