Skip to content

Commit

Permalink
Ruby 3.4 allows Strings as Fiber storage keys
Browse files Browse the repository at this point in the history
But not anything else that responds to #to_sym. It does accept objects that
respond to #to_str.
This expands upon the ruby_bug block from upstream MRI. This also restores the
previously removed "can't use invalid keys" test, but with the String key
removed.

Reference: https://bugs.ruby-lang.org/issues/20978
  • Loading branch information
herwinw authored and eregon committed Jan 7, 2025
1 parent cc693a3 commit 77ebfbf
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/fiber/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@
key = :"#{self.class.name}#.#{self.object_id}"
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
end

it "can't use invalid keys" do
invalid_keys = [Object.new, 12]
invalid_keys.each do |key|
-> { Fiber[key] }.should raise_error(TypeError)
end
end
end

ruby_bug "#20978", "3.2"..."3.4" do
Expand All @@ -98,6 +105,23 @@
def key.to_str; "Foo"; end
Fiber.new { Fiber[key] = 42; Fiber["Foo"] }.resume.should == 42
end

it "converts a String key into a Symbol" do
Fiber.new { Fiber["key"] = 42; Fiber[:key] }.resume.should == 42
Fiber.new { Fiber[:key] = 42; Fiber["key"] }.resume.should == 42
end

it "can use any object that responds to #to_str as a key" do
key = mock("key")
key.should_receive(:to_str).twice.and_return("key")
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
end
end

it "does not call #to_sym on the key" do
key = mock("key")
key.should_not_receive(:to_sym)
-> { Fiber[key] }.should raise_error(TypeError)
end

it "can access the storage of the parent fiber" do
Expand Down

0 comments on commit 77ebfbf

Please sign in to comment.