logo
  • 教程列表
  • SO官方文档
  • 方法缺失
    • 捕获对未定义方法的调用
    • 与块一起使用
    • 与参数一起使用
    • 使用缺少的方法
  1. StackOverflow 文档
  2. Ruby Language 教程
  3. 方法缺失
  4. 使用缺少的方法

使用缺少的方法

Created: November-22, 2018

class Animal
  def method_missing(method, *args, &block)
    say, speak = method.to_s.split("_")
    if say == "say"
      speak
    else
      super
    end
  end
end

=> a = Animal.new
=> a.say_moo
=> "moo"
=> a.shout_moo
=> NoMethodError: undefined method `shout_moo'
  • 与参数一起使用

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明