Last active
December 28, 2015 07:29
-
-
Save jamesalmond/7464398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'delegate' | |
class Child | |
def open | |
"HELLO WORLD" | |
end | |
def ping | |
"PING" | |
end | |
end | |
class Parent < SimpleDelegator | |
end | |
parent = Parent.new(Child.new) | |
puts parent.ping | |
puts parent.send(:ping) | |
puts parent.open | |
puts parent.send(:open) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby delegate.rb | |
PING | |
PING | |
HELLO WORLD | |
delegate.rb:21:in `initialize': wrong number of arguments (0 for 1..3) (ArgumentError) | |
from delegate.rb:21:in `open' | |
from delegate.rb:21:in `<main>' |
ping, send(:ping) and #open calls go via Delegator#method_missing but #send(:open) doesn't because Delegator includes a bunch of methods from Kernel
https://github.com/rubysl/rubysl-delegate/blob/2.0/lib/rubysl/delegate/delegate.rb#L44-L50
One of these methods is Kernel#open so send(:open) sees it in the inheritance chain for your parent and calls this directly instead of going via method_missing
You can "fix it":
require 'delegate'
class Delegator
undef_method :open
end
and file a bug report to ruby-dev I guess.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works if you use
public_send
instead ofsend
. There is a private method calledopen
somewhere in the inheritance chain that causes the error...