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.2.10 Released
Ruby 3.2.10 has been released.
Posted by hsbt on 14 Jan 2026
Ruby 4.0.1 Released
Ruby 4.0.1 has been released.
Posted by k0kubun on 13 Jan 2026
Ruby 4.0.0 Released
We are pleased to announce the release of Ruby 4.0.0. Ruby 4.0 introduces “Ruby Box” and “ZJIT”, and adds many improvements.
Posted by naruse on 25 Dec 2025
A New Look for Ruby's Documentation
Following the ruby-lang.org redesign, we have more news to celebrate Ruby’s 30th anniversary: docs.ruby-lang.org has a completely new look with Aliki—RDoc’s new default theme.
Posted by Stan Lo on 23 Dec 2025