I should be able to call Kernel methods on every object, and method format is defined on Kernel. Why is method_missing invoked on Kernel with the third example?
class A
def method_missing(meth, *args, &block)
if meth == :foo
puts 'ok'
elsif meth == :format
puts 'ok'
end
end
end
a = A.new
a.foo # => ok
a.send(:foo) # => ok
a.format # => ok
a.send(:format) # => too few arguments (ArgumentError)
That is because Kernel#format is a private method. When you call it using send, which means you are calling it without an explicit receiver, the defined method is called, and argument error is raised. When you call it with an explicit receiver, the method is not found because the defined one is private, and so method_missing is invoked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With