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
      return speak.upcase if args.first == "shout"
      speak
    else
      super
    end
  end
end

=> Animal.new.say_moo
=> "moo" 
=> Animal.new.say_moo("shout")
=> "MOO" 
  • 使用缺少的方法
  • 與塊一起使用

Copyright © 2018. All right reserved

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

  • 關於本站
  • 免責聲明