From eff15a269fdc37d2b09cf1dfe8c1b1bf6e377a32 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 26 Sep 2019 17:27:47 +0200 Subject: [EXPERIMENTAL] Make NilClass#to_s, TrueClass#to_s and FalseClass#to_s return a frozen String * Always the same frozen String for each of these values. * Avoids extra allocations whenever calling these 3 methods. * See [Feature #16150] --- spec/ruby/core/false/to_s_spec.rb | 10 ++++++++++ spec/ruby/core/nil/to_s_spec.rb | 10 ++++++++++ spec/ruby/core/true/to_s_spec.rb | 10 ++++++++++ 3 files changed, 30 insertions(+) (limited to 'spec/ruby/core') diff --git a/spec/ruby/core/false/to_s_spec.rb b/spec/ruby/core/false/to_s_spec.rb index 40853da88b..c996d87000 100644 --- a/spec/ruby/core/false/to_s_spec.rb +++ b/spec/ruby/core/false/to_s_spec.rb @@ -4,4 +4,14 @@ describe "FalseClass#to_s" do it "returns the string 'false'" do false.to_s.should == "false" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + false.to_s.frozen?.should == true + end + + it "always returns the same string" do + false.to_s.should equal(false.to_s) + end + end end diff --git a/spec/ruby/core/nil/to_s_spec.rb b/spec/ruby/core/nil/to_s_spec.rb index c7a18e2527..4b61f589f7 100644 --- a/spec/ruby/core/nil/to_s_spec.rb +++ b/spec/ruby/core/nil/to_s_spec.rb @@ -4,4 +4,14 @@ describe "NilClass#to_s" do it "returns the string ''" do nil.to_s.should == "" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + nil.to_s.frozen?.should == true + end + + it "always returns the same string" do + nil.to_s.should equal(nil.to_s) + end + end end diff --git a/spec/ruby/core/true/to_s_spec.rb b/spec/ruby/core/true/to_s_spec.rb index 30ca354b0c..68b0052c5d 100644 --- a/spec/ruby/core/true/to_s_spec.rb +++ b/spec/ruby/core/true/to_s_spec.rb @@ -4,4 +4,14 @@ describe "TrueClass#to_s" do it "returns the string 'true'" do true.to_s.should == "true" end + + ruby_version_is "2.7" do + it "returns a frozen string" do + true.to_s.frozen?.should == true + end + + it "always returns the same string" do + true.to_s.should equal(true.to_s) + end + end end -- cgit v1.2.3