Skip to content

Allow Hash#transform_keys to take a hash argument #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Compatibility:
* `Module#attr_*` methods now return an array of method names (#2498, @gogainda).
* Fixed `Socket#(local|remote)_address` to retrieve family and type from the file descriptor (#2444, @larskanis).
* Add `Thread.ignore_deadlock` accessor (#2453).
* Allow `Hash#transform_keys` to take a hash argument (@ccocchi, #2464).

Performance:

Expand Down
5 changes: 0 additions & 5 deletions spec/tags/core/hash/transform_keys_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
fails:Hash#transform_keys! returns the processed keys and non evaluated keys if we broke from the block
fails:Hash#transform_keys allows a hash argument
fails:Hash#transform_keys allows a partial transformation of keys when using a hash argument
fails:Hash#transform_keys allows a combination of hash and block argument
fails:Hash#transform_keys! allows a hash argument
fails:Hash#transform_keys! on frozen instance raises a FrozenError on hash argument
42 changes: 32 additions & 10 deletions src/main/ruby/truffleruby/core/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,47 @@ def transform_values!
self
end

def transform_keys
return to_enum(:transform_keys) { size } unless block_given?

def transform_keys(mapping = nil)
has_block = block_given?
h = {}
each_pair do |key, value|
h[yield(key)] = value

if mapping
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash)
each_pair do |key, value|
k = Primitive.hash_get_or_undefined(mapping, key)
k = has_block ? yield(key) : key if Primitive.undefined?(k)
h[k] = value
end
else
return to_enum(:transform_keys) { size } unless has_block

each_pair do |key, value|
h[yield(key)] = value
end
end

h
end

def transform_keys!
return to_enum(:transform_keys!) { size } unless block_given?
def transform_keys!(mapping = nil)
has_block = block_given?
return to_enum(:transform_keys!) { size } unless mapping || has_block

Primitive.check_frozen self

h = {}

begin
each_pair do |key, value|
h[yield(key)] = value
if mapping
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash)
each_pair do |key, value|
k = Primitive.hash_get_or_undefined(mapping, key)
k = has_block ? yield(key) : key if Primitive.undefined?(k)
h[k] = value
end
else
each_pair do |key, value|
h[yield(key)] = value
end
end
ensure
replace h
Expand Down
3 changes: 0 additions & 3 deletions tool/jt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1637,9 +1637,6 @@ def purge(path, *args)

def untag(path, *args)
require_ruby_launcher!
puts
puts "WARNING: untag is currently not very reliable - run `jt test #{[path,*args] * ' '}` after and manually annotate any new failures"
puts
test_specs('untag', path, *args)
end

Expand Down