Module Classes

module Authentication
AUTH_ENDPOINT = "http://example.com/auth"
def self.configure
# Configure the authentication
end
class Http
def self.get(url)
# Make a GET request
end
end
end

To use a module class, you would use the following syntax:

Authentication::Http.get("http://example.com/users")

Module Modules

And finally you can also have modules within modules:

module Authentication
AUTH_ENDPOINT = "http://example.com/auth"
def self.configure
# Configure the authentication
end
class Http
def self.get
# Make a GET request
end
end
module Serializers
class JsonSerializer
end
end
end

And predictably, you would use it like this.

Authentication::Serializers::JsonSerializer.new

Conclusion

Modules are a very useful part of the Ruby language and you will see them used to great effect within the Ruby community.

Namespacing your code is a good practice you should follow as it makes working within bigger projects much easier.

When you learn a new technique such as this, it’s easy to get carried away and go nuts. Generally speaking you should keep your modules pretty shallow.

In next week’s tutorial we will be continuing with out exploration of Modules to understand another important use-case.