💎

Ruby 3.2 - Hash

2022/12/15に公開

Ruby 3.2 アドベントカレンダーの15日目の記事です。

https://qiita.com/advent-calendar/2022/ruby32


Hash

デフォルト値を持つ Hash オブジェクトが空の場合に Hash#shift が返す値が変わった

Bug #16908: Strange behaviour of Hash#shift when used with default_proc. - Ruby master - Ruby Issue Tracking System

Hash オブジェクトが空の場合、Hash#shift は nil を返すけど、Hash オブジェクトがデフォルト値を持っていた場合はそのデフォルト値を返すようになっていた。

h = Hash.new(123)  #=> {}
h[:hoge]  #=> 123
h.shift   #=> 123

これは変だってことで、Ruby 3.2 ではデフォルト値があっても nil を返すようになった。

h = Hash.new(123)  #=> {}
h[:hoge]  #=> 123
h.shift   #=> nil

Discussion