ruby module_function vs including module

Viewed 27894

In ruby, I understand that module functions can be made available without mixing in the module by using module_function as shown here. I can see how this is useful so you can use the function without mixing in the module.

module MyModule
  def do_something
    puts "hello world"
  end
  module_function :do_something
end

My question is though why you might want to have the function defined both of these ways.

Why not just have

def MyModule.do_something

OR

def do_something

In what kind of cases would it be useful to have the function available to be mixed in, or to be used as a static method?

3 Answers
Related