From 2c15a579d52d8d0487f22a87f1fc428055c7eff0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 3 Nov 2026 19:52:59 +0100 Subject: [PATCH] Add specs for $/ and $-0 assigned a String equal to the default separator Ruby 4.0 stores the default separator object itself when the assigned String is equal to it, before the "frozen bare string is kept" rule applies: io.c:8734 deprecated_rs_setter if (rb_str_equal(val, rb_default_rs)) val = rb_default_rs; else val = rb_str_frozen_bare_string(val); The existing examples all use "abc", so none of them reaches the first branch. That matters because the two branches disagree for an input both could accept: a frozen, bare "\n" satisfies "frozen and has no instance variables" and is nevertheless NOT the object stored. Verified on ruby 4.0.7 (2026-09-15 revision 229531a6cf) [arm64-darwin25]: $/ = "\n".dup.freeze $/.equal?(the assigned string) # => false $/.equal?(the default) # => true language/predefined_spec.rb: 172 examples, 0 failures before; 174 examples, 0 failures after. Co-Authored-By: Claude Opus 5 --- language/predefined_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/language/predefined_spec.rb b/language/predefined_spec.rb index a78ef9c76..e8d7f739b 100644 --- a/language/predefined_spec.rb +++ b/language/predefined_spec.rb @@ -720,6 +720,15 @@ def foo $/ = str $/.should.equal?(str) end + + it "assigns the default separator if the given String is equal to it" do + default = "\n" + str = "\n".dup.freeze + $/ = str + $/.should_not.equal?(str) + $/.should.frozen? + $/.should == default + end end it "can be assigned nil" do @@ -801,6 +810,15 @@ def foo $-0 = str $-0.should.equal?(str) end + + it "assigns the default separator if the given String is equal to it" do + default = "\n" + str = "\n".dup.freeze + $-0 = str + $-0.should_not.equal?(str) + $-0.should.frozen? + $-0.should == default + end end it "can be assigned nil" do