Skip to content

Commit 5e3c15d

Browse files
committed
[en,ja]Add regression-of-hash-reject-in-ruby-2-1-1
Solves #665
1 parent fc08cb5 commit 5e3c15d

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+
```
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+
```
22+
23+
(To be exact, extra states such as instance variables and other aren't copied either.)
24+
25+
With the release of Ruby 2.1.0 we've [changed our policy](https://www.ruby-lang.org/en/news/2013/12/21/semantic-versioning-after-2-1-0/), so Ruby 2.1.1 shouldn't include such behavior changes.
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 will be fixed in Ruby 2.1.2, but this change is still scheduled 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 happened 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 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` メソッドはその継承したクラスのオブジェクトを返す挙動になっていました。しかし、Ruby 2.1.1 で意図せずその挙動が変更され、継承したクラスの場合でも必ず Hash クラスのオブジェクト帰る挙動に変更されてしまっています。
11+
12+
```
13+
class SubHash < Hash
14+
end
15+
16+
p Hash.new.reject { }.class
17+
#=> 2.1.0: Hash, 2.1.1: Hash
18+
p SubHash.new.reject { }.class
19+
#=> 2.1.0: SubHash, 2.1.1: Hash
20+
```
21+
22+
(正確にはインスタンス変数など他の属性もコピーされなくなっています。)
23+
24+
Ruby 2.1.0 から[バージョニングポリシーを変更している](https://www.ruby-lang.org/ja/news/2013/12/21/semantic-versioning-after-2-1-0/)ため、本来 Ruby 2.1.1 にこのような挙動の変更が入るべきではありません。
25+
26+
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
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)