Ruby - Module :: NoMethodError

Sayuj picture Sayuj · Jun 1, 2011 · Viewed 22.1k times · Source

I have a module like this:

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

And I am trying to access this from my main program like this:

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

But it generate error:

undefined method `probe_invoke' for Prober:Module (NoMethodError)

Answer

rubyprince picture rubyprince · Jun 1, 2011

Apart from the answers that give you the option of defining the function as self., you have another option of including the module and calling it without the module reference like this:

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

and you can call it like this:

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