ruby / ruby/ostruct

OpenStruct#respond_to? when true fails with NameError

Open
#55 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
175
Forks
35
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Hello,

I have a case where I overrode respond_to? on a subclass of OpenStruct because I this class to work as fake object.

class Dep
  def a
    "Hello from Dep"
  end
end
class FakeDep < OpenStruct
  def respond_to?(*args)
    super || Dep.new.respond_to?(*args)
  end  
end

FakeDep.new(a: [])  #=> NameError: undefined method `a' for class `FakeDep'

      owner = method!(name).owner
              ^^^^^^^
from /usr/local/lib/ruby/3.1.0/ostruct.rb:245:in `method'

I didn't fully understand https://github.com/ruby/ruby/commit/f48edc28dda3df962f289fd373c06a8dfeda3dc0 and what happens with owner = method!(name).owner. Is the error above a valid case?

TIA

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the FakeDep example against the OpenStruct implementation at ostruct.rb around line 245, then read the linked commit to understand the method! and owner logic. Compare the observed NameError with the intended respond_to? behavior; done means establishing whether this is a supported bug and identifying the regression coverage or behavior change needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.