[ruby koan] about_message_passing 消息传递

[ruby koan] about_message_passing 消息传递

向对象发送消息

当你给一个对象发消息时,对象从 它的方法查询表 method lookup path 中查找第一个名字匹配的方法,并执行该方法;

如果未找到匹配的方法

  • 产生 NoMethodError 异常;

  • 除非你为对象提供了 method_missing 方法;method_missing 接收 三个参数:

    • 未定义方法的 symbol

    • 原始调用的参数列表 args

    • 传递给原始调用的任意代码块 block

method_missing 在某种程度上是一个安全层 :它为您提供了一种拦截无法响应的消息并优雅地处理它们的方法。

class Dummy  
  def method_missing(m, *args, &block)  
    puts "There's no method called #{m} here -- please try again."  
  end  
end  
Dummy.new.anything
>ruby p012zmm.rb  
There's no method called anything here -- please try again.  
>Exit code: 0  

Reference: http://rubylearning.com/satishtalim/ruby_method_missing.html

类方法的2种调用方式

  • 直接调用

  • 发送消息调用

    • 发消息调用的好处是,可以更动态地生成要调用的方法名

    • 两个方法: send(msg)__send__(msg)

      • 为什么ruby要定义2个方法:因为很多类会自定义 send, 第二个方法备不时之需

    • 与直接调用是等价的

      • 可以传递参数

      • 未定义时,也报错 NoMethodError

    • respond_to?(:methodName) 查询是否支持响应methodName

class MessageCatcher
    def caught?
      true
    end
  end
​
  mc = MessageCatcher.new
  
  #直接调用
  mc.caught?
  #发消息调用
  mc.send(:caught?)
  # 发消息调用的灵活使用示例:
  mc.send("caught?")
  mc.send("caught" + "?" )
  mc.send("CAUGHT?".downcase ) 

消息捕获

  • 重写 method_missing(method_name, *args, &block) 捕获发给本对象的送来消息

    • 所有消息都会被 method_missing 捕获

    • 可以 使用 if else super(...) 对部分消息做处理

  • 注意:如果 method_missing 产生 NoMethodError,会陷入无限递归

class AllMessageCatcher
    def method_missing(method_name, *args, &block)
      "Someone called #{method_name} with <#{args.join(", ")}>"
    end
  end
​
  # 所有的消息都能被捕捉
  def test_all_messages_are_caught
    catcher = AllMessageCatcher.new
​
    assert_equal "Someone called foobar with <>", catcher.foobar
    assert_equal "Someone called foobaz with <1>", catcher.foobaz(1)
    assert_equal "Someone called sum with <1, 2, 3, 4, 5, 6>", catcher.sum(1,2,3,4,5,6)
  end
​
  # 捕捉消息,制造  respond_to 谎言
  def test_catching_messages_makes_respond_to_lie
    catcher = AllMessageCatcher.new
​
    assert_nothing_raised do
      catcher.any_method
    end
    assert_equal false, catcher.respond_to?(:any_method)
  end
# 捕捉 以foo为前缀 的 消息
  # 不是 以foo为前缀的消息,照常判断,不存在则报 NoMethodError
  class WellBehavedFooCatcher
    def method_missing(method_name, *args, &block)
      if method_name.to_s[0,3] == "foo"
        "Foo to you too"
      else
        super(method_name, *args, &block)
      end
    end
  end
​
  def test_foo_method_are_caught
    catcher = WellBehavedFooCatcher.new
​
    assert_equal "Foo to you too", catcher.foo_bar
    assert_equal "Foo to you too", catcher.foo_baz
  end
​
  def test_non_foo_messages_are_treated_normally
    catcher = WellBehavedFooCatcher.new
​
    assert_raise(NoMethodError) do
      catcher.normal_undefined_method
    end
  end
 
考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值