Hash#selectとHash#rejectの意外な相違点
rejectはデフォルト値を受け継ぐが,selectは受け継がない.
Hash.new(42).select { nil }[0] # => nil Hash.new(42).reject { nil }[0] # => 42
リファレンスマニュアルの説明の微妙な違いは,この挙動の違いから来るものなのか?
key, value のペアについてブロックを評価し,真となるペアだけを含むハッシュを生成して返します。
http://doc.okkez.net/static/192/method/Hash/i/select.html
self を複製して、ブロックを評価した値が真になる要素を削除したハッシュを返します。
http://doc.okkez.net/static/192/method/Hash/i/reject.html
(追記)
selectでは compare_by_identity?
も受け継がない
{}.compare_by_identity.select { nil }.compare_by_identity? # => false {}.compare_by_identity.reject { nil }.compare_by_identity? # => true