开发者

Why don't I get a method error?

开发者 https://www.devze.com 2023-01-30 15:22 出处:网络
I\'m a rails programmer. And rails always 开发者_开发技巧tells me that I\'ve got \"NoMethodError\". Why don\'t I get a MethodError? I want a MethodError.

I'm a rails programmer. And rails always 开发者_开发技巧tells me that I've got "NoMethodError". Why don't I get a MethodError? I want a MethodError.

Please help me.

Edit:

Here's my code:

42.this_is_a_method


Ruby doesn't have a MethodError. But you can make one!

class MethodError < NoMethodError; end

Now, catch all the NoMethodErrors in this way:

class Object
  def method_missing(m)
    raise MethodError, "undefined method `#{m}' for #{self.inspect}:#{self.class}"
  end
end


You would be getting a NoMethodError because the Fixnum class doesn't have a this_is_a_method defined for instances of that class.

0

精彩评论

暂无评论...
验证码 换一张
取 消