Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel.y interferes with code relying on method_missing #664

Open
dysonreturns opened this issue Dec 18, 2023 · 3 comments
Open

Kernel.y interferes with code relying on method_missing #664

dysonreturns opened this issue Dec 18, 2023 · 3 comments

Comments

@dysonreturns
Copy link

Good day,

I have coordinate objects in protobuf which have a .y attribute defined, but rely on method_missing to trigger calls to C.
Kernel.y intercepts the message and gives me an unexpected result.
I believe other systems which also rely on method_missing like nokogiri will cause the same trouble when trying to access an arbitrary "y" property in a document.

Can we rename the "y" patch to Kernel, to something less common, please?

def y *objects

Otherwise people who use x,y,z coordinates get adverse effects and have to implement a workaround like the following.

    def y
      send(:method_missing, :y)
    end

Even "yy" would be better. Thank you :)

@amomchilov
Copy link
Contributor

amomchilov commented Dec 20, 2023

This would make sense. #y seems like to common of a name to override. Removing it today would be a breaking change though, and wouldn't be appropriate without a new major version release.

Could you workaround it by adding this shim for yourself?

def y = method_missing(:y)

@tenderlove
Copy link
Member

Any idea who is requiring that file? It only adds the method to Kernel if something is requiring it, and Psych does not require it by default.

$ ruby -rpsych -e'y Object.new'
-e:1:in `<main>': undefined method `y' for main (NoMethodError)

y Object.new
^

I'm not thrilled with Psych adding Kernel#y (it's inherited as a legacy feature, and I'm open to a discussion about removing it), but since you have to specifically require psych/y for it to happen, it seems like that's the place to fix.

@dysonreturns
Copy link
Author

dysonreturns commented Jan 19, 2024

Thanks for your time. I should've added steps to reproduce in that moment, because my scenario eludes me today.

It would appear that this issue only occurs when IRB is defined.

if defined?(::IRB)

So I must have been either inside bundler's gem-templated bin/console (which fires up irb) or maybe some past version of RubyMine's debugger.

Found some fresh steps to reproduce:

$ irb
irb(main):001> y Object.new
(irb):1:in `<main>': undefined method `y' for main (NoMethodError)
	from <internal:kernel>:187:in `loop'
[ Truncated... ]
irb(main):002> require "yaml" # or require "psych" 
=> true
irb(main):003> y Object.new
--- !ruby/object {}
=> nil

I'm feeling less strong about it now, since in all likelyhood it doesn't effect anyone's live environments.
However, if anyone ever requires anything which defines ::IRB by happenstance, there could be production implications for #y.

Feel free to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants