diff --git a/lib/ostruct.rb b/lib/ostruct.rb index a8763da..ecf37a9 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -252,12 +252,13 @@ def new_ostruct_member!(name) # :nodoc: private :new_ostruct_member! private def is_method_protected!(name) # :nodoc: - if !respond_to?(name, true) + sc = singleton_class! + if !(sc.method_defined?(name) || sc.private_method_defined?(name)) false elsif name.match?(/!$/) true else - owner = method!(name).owner + owner = sc.instance_method(name).owner if owner.class == ::Class owner < ::OpenStruct else diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 616f84d..f0cbd7d 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -439,4 +439,10 @@ def test_performance_warning ) end end + def test_initialize_with_respond_to_field + o = OpenStruct.new(respond_to?: false, next_field: 3) + + assert_equal({respond_to?: false, next_field: 3}, o.to_h) + end + end