File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed
Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -2124,7 +2124,7 @@ rb_fiber_storage_set(VALUE self, VALUE value)
21242124static VALUE
21252125rb_fiber_storage_aref (VALUE class , VALUE key )
21262126{
2127- Check_Type ( key , T_SYMBOL );
2127+ key = rb_to_symbol ( key );
21282128
21292129 VALUE storage = fiber_storage_get (fiber_current (), FALSE);
21302130 if (storage == Qnil ) return Qnil ;
@@ -2145,7 +2145,7 @@ rb_fiber_storage_aref(VALUE class, VALUE key)
21452145static VALUE
21462146rb_fiber_storage_aset (VALUE class , VALUE key , VALUE value )
21472147{
2148- Check_Type ( key , T_SYMBOL );
2148+ key = rb_to_symbol ( key );
21492149
21502150 VALUE storage = fiber_storage_get (fiber_current (), value != Qnil );
21512151 if (storage == Qnil ) return Qnil ;
Original file line number Diff line number Diff line change 9090 key = :"#{ self . class . name } #.#{ self . object_id } "
9191 Fiber . new { Fiber [ key ] = 42 ; Fiber [ key ] } . resume . should == 42
9292 end
93+ end
9394
95+ ruby_version_is "3.2.3" ..."3.4" do
9496 it "can't use invalid keys" do
9597 invalid_keys = [ Object . new , "Foo" , 12 ]
9698 invalid_keys . each do |key |
99101 end
100102 end
101103
104+ ruby_version_is "3.4" do
105+ it "can use keys as strings" do
106+ key = Object . new
107+ def key . to_str ; "Foo" ; end
108+ Fiber [ key ] = 42
109+ Fiber [ "Foo" ] . should == 42
110+ end
111+ end
112+
102113 it "can access the storage of the parent fiber" do
103114 f = Fiber . new ( storage : { life : 42 } ) do
104115 Fiber . new { Fiber [ :life ] } . resume
Original file line number Diff line number Diff line change @@ -252,6 +252,18 @@ def tvar(var, val)
252252 assert_equal ( nil , Thread . current [ :v ] ) ;
253253 end
254254
255+ def test_fiber_variables
256+ assert_equal "bar" , Fiber . new { Fiber [ :foo ] = "bar" ; Fiber [ :foo ] } . resume
257+
258+ key = :"#{ self . class . name } #.#{ self . object_id } "
259+ Fiber [ key ] = 42
260+ assert_equal 42 , Fiber [ key ]
261+
262+ key = Object . new
263+ def key . to_str ; "foo" ; end
264+ assert_equal "Bar" , Fiber . new { Fiber [ key ] = "Bar" ; Fiber [ key ] } . resume
265+ end
266+
255267 def test_alive
256268 fib = Fiber . new { Fiber . yield }
257269 assert_equal ( true , fib . alive? )
You can’t perform that action at this time.
0 commit comments