Skip to content

Commit 3f62717

Browse files
committed
Move boolean condition check outside of loop
1 parent f4725c2 commit 3f62717

File tree

1 file changed

+17
-12
lines changed
  • src/main/ruby/truffleruby/core

1 file changed

+17
-12
lines changed

src/main/ruby/truffleruby/core/hash.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,20 +541,23 @@ def transform_values!
541541

542542
def transform_keys(mapping = nil)
543543
has_block = block_given?
544-
return to_enum(:transform_keys) { size } unless mapping || has_block
545-
546-
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash) if mapping
547-
548544
h = {}
549-
each_pair do |key, value|
550-
if mapping
545+
546+
if mapping
547+
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash)
548+
each_pair do |key, value|
551549
k = Primitive.hash_get_or_undefined(mapping, key)
552550
k = has_block ? yield(key) : key if Primitive.undefined?(k)
553551
h[k] = value
554-
else
552+
end
553+
else
554+
return to_enum(:transform_keys) { size } unless has_block
555+
556+
each_pair do |key, value|
555557
h[yield(key)] = value
556558
end
557559
end
560+
558561
h
559562
end
560563

@@ -563,16 +566,18 @@ def transform_keys!(mapping = nil)
563566
return to_enum(:transform_keys!) { size } unless mapping || has_block
564567

565568
Primitive.check_frozen self
566-
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash) if mapping
567-
568569
h = {}
570+
569571
begin
570-
each_pair do |key, value|
571-
if mapping
572+
if mapping
573+
mapping = Truffle::Type.coerce_to(mapping, Hash, :to_hash)
574+
each_pair do |key, value|
572575
k = Primitive.hash_get_or_undefined(mapping, key)
573576
k = has_block ? yield(key) : key if Primitive.undefined?(k)
574577
h[k] = value
575-
else
578+
end
579+
else
580+
each_pair do |key, value|
576581
h[yield(key)] = value
577582
end
578583
end

0 commit comments

Comments
 (0)