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)
You can define and access instance variables within a module's instance methods, but you can't actually instantiate a module.
A user cannot access instance method directly with the use of the dot operator as he cannot make the instance of the module. To access the instance method defined inside the module, the user has to include the module inside a class and then use the class instance to access that method.
Raised when a method is called on a receiver which doesn't have it defined and also fails to respond with method_missing .
What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With