Skip to content

Commit cde4cfa

Browse files
committed
[GR-18163] Fix Hash#{to_s,inspect} for keys whose #inspect return a frozen String
PullRequest: truffleruby/3218
2 parents ca9a132 + dd655c7 commit cde4cfa

File tree

6 files changed

+10
-22
lines changed

6 files changed

+10
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Bug fixes:
1515
* Fix a resource leak from allocators defined in C extensions (@aardvark179).
1616
* `SIGINT`/`Interrupt`/`Ctrl+C` now shows the backtrace and exits as signaled, like CRuby (@eregon).
1717
* Update patch feature finding to prefer the longest matching load path (#2605, @bjfish).
18+
* Fix `Hash#{to_s,inspect}` for keys whose `#inspect` return a frozen String (#2613, @eregon).
1819

1920
Compatibility:
2021

spec/ruby/core/hash/shared/to_s.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require_relative '../fixtures/classes'
33

44
describe :hash_to_s, shared: true do
5-
65
it "returns a string representation with same order as each()" do
76
h = { a: [1, 2], b: -2, d: -6, nil => nil }
87

@@ -95,4 +94,8 @@
9594

9695
{a: utf_16be}.send(@method).should == '{:a=>"utf_16be \u3042"}'
9796
end
97+
98+
it "works for keys and values whose #inspect return a frozen String" do
99+
{ true => false }.to_s.should == "{true=>false}"
100+
end
98101
end

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,14 @@ def insert(idx, *items)
602602
Truffle::Graal.always_split instance_method(:insert)
603603

604604
def inspect
605-
return '[]'.encode(Encoding::US_ASCII) if size == 0
606-
comma = ', '
605+
return '[]'.encode(Encoding::US_ASCII) if empty?
607606
result = +'['
608607

609608
return +'[...]' if Truffle::ThreadOperations.detect_recursion self do
610609
each_with_index do |element, index|
611610
temp = Truffle::Type.rb_inspect(element)
612611
result.force_encoding(temp.encoding) if index == 0
613-
result << temp << comma
612+
result << temp << ', '
614613
end
615614
end
616615

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929
class FalseClass
30-
3130
def to_s
3231
'false'
3332
end
34-
35-
def inspect
36-
# Call #to_s rather than alias it so that people can change #to_s if they
37-
# wish.
38-
to_s
39-
end
33+
alias_method :inspect, :to_s
4034

4135
alias_method :===, :===
4236

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,7 @@ def inspect
420420
out = []
421421
return +'{...}' if Truffle::ThreadOperations.detect_recursion self do
422422
each_pair do |key,value|
423-
str = Truffle::Type.rb_inspect(key)
424-
str << '=>'
425-
str << Truffle::Type.rb_inspect(value)
426-
out << str
423+
out << "#{Truffle::Type.rb_inspect(key)}=>#{Truffle::Type.rb_inspect(value)}"
427424
end
428425
end
429426

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929
class TrueClass
30-
3130
def to_s
3231
'true'
3332
end
34-
35-
def inspect
36-
# Call #to_s rather than alias it so that people can change #to_s if they
37-
# wish.
38-
to_s
39-
end
33+
alias_method :inspect, :to_s
4034

4135
alias_method :===, :===
4236

0 commit comments

Comments
 (0)