¿Por qué se busca el método Kernel solo cuando se usa `send`?
Frecuentes
Visto 71 equipos
5
debería poder llamar Kernel
methods on every object, and method format
se define en Kernel
. Por que es method_missing
invocado en 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)
1 Respuestas
7
Eso es porque 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
se invoca.
contestado el 22 de mayo de 14 a las 13:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas ruby or haz tu propia pregunta.
Changed accepted answer to this one, as it actually explains the
send
vs. direct call mismatch better. - malte rohde