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.

Reference: https://bugs.ruby-lang.org/issues/20978
  • Loading branch information
herwinw committed Dec 31, 2024
1 parent ecb47be commit c3ce0f1
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion core/fiber/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,51 @@
end

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

ruby_version_is ""..."3.4" do
it "can't use String as a key" do
-> { Fiber["Foo"] }.should raise_error(TypeError)
end

it "can't use an object that responds to #to_str as a key" do
key = mock("key")
key.should_not_receive(:to_str)
-> { Fiber[key] }.should raise_error(TypeError)
end
end
end

ruby_version_is "3.4" do
it "can use String as a key" do
Fiber["Foo"] = 123
Fiber["Foo"].should == 123
end

it "converts a String key into a Symbol" do
Fiber["key"] = 123
Fiber[:key].should == 123

Fiber[:other_key] = 123
Fiber["other_key"].should == 123
end

it "can use #to_str as a key" do
key = mock("key")
key.should_receive(:to_str).twice.and_return("key")
Fiber[key] = 123
Fiber[key].should == 123
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 c3ce0f1

Please sign in to comment.