Instance methods in modules

Viewed 16715

Consider the following code:

module ModName
  def aux
    puts 'aux'
  end
end

If we replace module with class, we can do the following:

ModName.new.aux

Modules cannot be instanced, though. Is there a way to call the aux method on the module?

6 Answers

You can do it this way:

module ModName
  def aux
    puts 'aux'
  end
  module_function :aux
end

and then just call it:

ModName.aux
Related