Private module methods in Ruby
I think the best way (and mostly how existing libs are written) to do this is by creating a class within the module that deals with all the logic, and the module just provides a convenient method, e.g.
module GTranslate
class Translator
def perform(text)
translate(text)
end
private
def translate(text)
# do some private stuff here
end
end
def self.translate(text)
t = Translator.new
t.perform(text)
end
end
There's also Module.private_class_method
, which arguably expresses more intent.
module Foo
def self.included(base)
base.instance_eval do
def method_name
# ...
end
private_class_method :method_name
end
end
end
For the code in the question:
module Thing
def self.pub; puts "Public method"; end
def self.priv; puts "Private method"; end
private_class_method :priv
end
Ruby 2.1 or newer:
module Thing
def self.pub; puts "Public method"; end
private_class_method def self.priv; puts "Private method"; end
end