Project

General

Profile

Actions

Bug #17735

closed

`Hash#transform_keys!` drops non evaluated keys

Added by kachick (Kenichi Kamiya) 26 days ago. Updated about 4 hours ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
[ruby-core:102949]

Description

hash = {a: 1, b: 2, c: 3}
hash.transform_values!(){ raise } rescue
p hash #=> {:a=>1, :b=>2, :c=>3}
hash = {a: 1, b: 2, c: 3}
hash.transform_keys!(){ raise } rescue
p hash #=> {}

Is this an intentional behavior?
I expected behavior like transform_values! in transform_keys! (Keeping non evaluated key-value pairs when exiting the block).

PR: https://github.com/ruby/ruby/pull/4294

Updated by nobu (Nobuyoshi Nakada) 26 days ago

These are just a bug, not a spec.

  ruby_version_is ""..."2.5.1" do
    it "partially modifies the contents if we broke from the block" do
      @hash.transform_keys! do |v|
        break if v == :c
        v.succ
      end
      @hash.should == { c: 1, d: 4 }
    end
  end

  ruby_version_is "2.5.1" do
    it "returns the processed keys if we broke from the block" do
      @hash.transform_keys! do |v|
        break if v == :c
        v.succ
      end
      @hash.should == { b: 1, c: 2 }
    end
  end
Actions #2

Updated by kachick (Kenichi Kamiya) 18 days ago

  • Status changed from Open to Closed

Applied in changeset git|31e0382723bfb35cffe3ca485dd0577668cafa07.


Keep non evaluated keys in Hash#transform_keys! [Bug #17735]

Updated by nagachika (Tomoyuki Chikanaga) 18 days ago

  • Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN to 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: REQUIRED, 3.0: REQUIRED

I have confirmed that the issue is reproducible on 2.6/2.7 too.

Updated by nagachika (Tomoyuki Chikanaga) about 4 hours ago

  • Backport changed from 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: REQUIRED, 3.0: REQUIRED to 2.5: UNKNOWN, 2.6: REQUIRED, 2.7: REQUIRED, 3.0: DONE

ruby_3_0 84d9a9afc0b49d095541acb9832f8b12fb506e19 merged revision(s) 31e0382723bfb35cffe3ca485dd0577668cafa07,5e5fb72f99701dc27c66ab148471893f14e6d6f0,fb6ebe55d91187d9635e0183d47dbf38e95b1141,522d4cd32f7727886f4fcbc28ed29c08d361ee20.

Actions

Also available in: Atom PDF