Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Module :: NoMethodError

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)

like image 329
Sayuj Avatar asked Jun 01 '11 07:06

Sayuj


People also ask

Can you instantiate a module in Ruby?

You can define and access instance variables within a module's instance methods, but you can't actually instantiate a module.

How do you access a module method in Ruby?

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.

What is Nomethoderror in Ruby?

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 class and module in Ruby?

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).


1 Answers

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) 
like image 103
rubyprince Avatar answered Sep 19 '22 13:09

rubyprince