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

Remove dependence on #method being defined #423

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amomchilov
Copy link

This PR tweaks how #405 was implemented, so that it achieves the same effect, but without needing the inspected object to implement #method.

This is done by using Kernel's implementation of #method, and using it with any kind of object.

I ran into this from trying to make a BasicObject subclass awesome-printable. To make it work, you need to define #ai, #class, #object_id and #method. After this change, only the first of those 3 are necessary.

# A class (ex. `Net::HTTP.Get`) might have `attr_reader :method` accessor
# which causes `object.method(:to_hash)` throw `ArgumentError (wrong number
# of arguments (given 1, expected 0))`. The following tries to avoid that.
return nil if KERNEL_METHOD.bind_call(object, :to_hash).arity != 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnboundMethod#bind_call was added in Ruby 2.7. For backwards compatibility, I'll need to switch to:

Suggested change
return nil if KERNEL_METHOD.bind_call(object, :to_hash).arity != 0
return nil if KERNEL_METHOD.bind(object).call(:to_hash).arity != 0

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

Successfully merging this pull request may close these issues.

1 participant