ITeye博客

Module方法调用好不好

夜鸣猪 · 2011-11-20 01:58 · RubyOnRails · 编程语言 · Markdown

以前说,用module给class加singleton方法,和extend到类include到类的instance 现在看看这个


module Prober
  def probe_invoke(type, data = {})
    p = Probe.new({:probe_type => type.to_s,
        :data => data.to_json, :probe_status => 0, :retries => 0})
    p.save
  end
end


掉的时候


require 'prober'
include Prober
probe_invoke("send_sms", sms_text)


module Prober
  def Prober.probe_invoke(type, data = {})
    p = Probe.new({:probe_type => type.to_s,
        :data => data.to_json, :probe_status => 0, :retries => 0})
    p.save
  end
end

Prober.probe_invoke("send_sms", sms_text)



module Prober

  class << self

    def probe_invoke(type, data = {})
      p = Probe.new({:probe_type => type.to_s,
          :data => data.to_json, :probe_status => 0, :retries => 0})
      p.save
    end

    # more module instance methods ...

  end

end


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。