Next to the answers here you also can do the following:
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
The class << self
block will define also every method in it as instance methods of your module.
(It's functionality is the same like to define every single methods by def Prober.mymethod ...
or def self.mymethod ...
)
Update (2014-11-22)
According to the Ruby Style Guide you should use module_function
instead:
module Prober
module_function # <-- preferred style nowadays
def probe_invoke(type, data = {})
Probe.new(probe_type: type.to_s,
data: data.to_json,
probe_status: 0,
retries: 0)
.save # no need for a temporary variable
end
# more module methods ...
end
I'd call this utility modules.
BTW: In the past it was more common to use extend self
instead of wrapping the methods in a class << self
block.
I also adapted the code above to other style guide recommendations.