D
D
Dgeremmi2014-05-19 15:43:49
ruby
Dgeremmi, 2014-05-19 15:43:49

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 не срабатывает

Why doesn't method_missing work on the last line?
ruby version 1.9.3
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Marat Amerov, 2014-05-19
@Dgeremmi

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 question

Ask a Question

731 491 924 answers to any question