Sometimes you want to write a batch of utility methods that can be accessed from a module for example Utility.parse_something(string) or any number of useful little tools for your application. Here’s a very clean-looking way to achieve it:

module Utilities extend self def parse_something(string) # do stuff here end def other_utility_method(number, string) # do some more stuff end end 1 module Utilities extend self def parse_something ( string ) # do stuff here end def other_utility_method(number, string) # do some more stuff end end

By placing "extend self" at the top of the module, you are telling it to include all of the methods defined on your module as class methods on…your module. Which means that you can now access them like other class methods! While there are other ways to do this, I think this is a very semantically clean way that gets it done quite nicely.

Update: And this is exactly why I post these Quick Tips, because sometimes there’s another solution that I hadn’t heard of! Using Ruby’s module_function you can achieve the same effect:

module Utilities module_function def parse_something(string) # do stuff here end def other_utility_method(number, string) # do some more stuff end end 1 module Utilities module_function def parse_something ( string ) # do stuff here end def other_utility_method(number, string) # do some more stuff end end