mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/ostruct] Allow properties to be accessed even when the object is moved to another Ractor (https://github.com/ruby/ostruct/pull/29)
https://github.com/ruby/ostruct/commit/d85639f2f5
This commit is contained in:
parent
83662f1d99
commit
cefa029573
2 changed files with 20 additions and 2 deletions
|
@ -221,8 +221,14 @@ class OpenStruct
|
|||
#
|
||||
def new_ostruct_member!(name) # :nodoc:
|
||||
unless @table.key?(name) || is_method_protected!(name)
|
||||
define_singleton_method!(name) { @table[name] }
|
||||
define_singleton_method!("#{name}=") {|x| @table[name] = x}
|
||||
getter_proc = Proc.new { @table[name] }
|
||||
setter_proc = Proc.new {|x| @table[name] = x}
|
||||
if defined?(::Ractor)
|
||||
::Ractor.make_shareable(getter_proc)
|
||||
::Ractor.make_shareable(setter_proc)
|
||||
end
|
||||
define_singleton_method!(name, &getter_proc)
|
||||
define_singleton_method!("#{name}=", &setter_proc)
|
||||
end
|
||||
end
|
||||
private :new_ostruct_member!
|
||||
|
|
|
@ -368,6 +368,18 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||
RUBY
|
||||
end if defined?(Ractor)
|
||||
|
||||
def test_access_methods_from_different_ractor
|
||||
assert_ractor(<<~RUBY, require: 'ostruct')
|
||||
os = OpenStruct.new
|
||||
os.value = 100
|
||||
r = Ractor.new(os) do |x|
|
||||
v = x.value
|
||||
Ractor.yield v
|
||||
end
|
||||
assert 100 == r.take
|
||||
RUBY
|
||||
end if defined?(Ractor)
|
||||
|
||||
def test_legacy_yaml
|
||||
s = "--- !ruby/object:OpenStruct\ntable:\n :foo: 42\n"
|
||||
o = YAML.safe_load(s, permitted_classes: [Symbol, OpenStruct])
|
||||
|
|
Loading…
Add table
Reference in a new issue