Regression of Hash#reject in Ruby 2.1.1
Posted by sorah on 10 Mar 2014
In Ruby 2.1.0 or earlier, the reject method in any class that inherits
Hash returns an object of its own class.
But in Ruby 2.1.1, this behavior has changed accidentally to return always
a plain Hash object, not of the inherited class.
class SubHash < Hash
end
p Hash.new.reject { }.class
#=> 2.1.0: Hash, 2.1.1: Hash
p SubHash.new.reject { }.class
#=> 2.1.0: SubHash, 2.1.1: Hash(To be exact, extra states such as instance variables, etc. aren’t copied either.)
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, so Ruby 2.1.1 is a patch level release and it shouldn’t break backwards compatibility.
This regression could potentially affect many libraries, one such case is
Rails’ HashWithIndifferentAccess and OrderedHash. They are broken:
Rails’ issue #14188.
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. So we recommend to fix your code in order to expect this behavior change.
This accident is caused by one missing backport commit. For more details, see http://blog.sorah.jp/2014/03/10/hash-reject-regression-in-ruby211.
Sorry for any inconvenience, and thank you for your support.
Recent News
Ruby 3.3.12 Released
Ruby 3.3.12 has been released.
Posted by hsbt on 16 Jul 2026
Ruby 4.0.6 Released
Ruby 4.0.6 has been released.
Posted by k0kubun on 14 Jul 2026
Ruby 3.4.10 Released
Ruby 3.4.10 has been released.
Posted by nagachika on 30 Jun 2026
Ruby 4.0.5 Released
Ruby 4.0.5 has been released.
Posted by k0kubun on 20 May 2026