From 9c269316357a1a5b6ef27794f3c9412b2e33a6cd Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Thu, 9 Dec 2021 04:06:53 +0900 Subject: [PATCH] [ruby/ostruct] `Proc`'s self should be shareable. To fix the issue https://bugs.ruby-lang.org/issues/18243 we need to make sure the Proc's self is shareable. These procs are used by `define_method` and it doesn't use Proc's self, so `nil` is enough. --- lib/ostruct.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 4f0a4a7dbe..ec44a525d9 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -221,11 +221,14 @@ class OpenStruct # def new_ostruct_member!(name) # :nodoc: unless @table.key?(name) || is_method_protected!(name) - getter_proc = Proc.new { @table[name] } - setter_proc = Proc.new {|x| @table[name] = x} if defined?(::Ractor) + getter_proc = nil.instance_eval{ Proc.new { @table[name] } } + setter_proc = nil.instance_eval{ Proc.new {|x| @table[name] = x} } ::Ractor.make_shareable(getter_proc) ::Ractor.make_shareable(setter_proc) + else + getter_proc = Proc.new { @table[name] } + setter_proc = Proc.new {|x| @table[name] = x} end define_singleton_method!(name, &getter_proc) define_singleton_method!("#{name}=", &setter_proc)