Answer the question
In order to leave comments, you need to log in
Why doesn't method_missing fire for method('some non-existent method').call?
Hi,
ruby has a special method called method_missing, in case you try to call a method that doesn't exist. Those. in such situation
class SampleClass
attr_accessor :something
def initialize(something)
@something = something
end
def method_missing(m,*args,&block)
p "#{m} doesn't exist"
end
end
e = SampleClass.new("wazzup!!")
p e.something #выведет wazzup!!
p e.foo #вызовет исключение NoMethodError и вызовет метод method_missing, выведет foo doesn't exist
e.method("foo").call #ошибка undefined method 'foo'. method_missing не срабатывает
Answer the question
In order to leave comments, you need to log in
try e.send(:foo)
method - this is a method of the Kernel class
, it looks like, in this case, method_missing does not participate in the call stack.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question