0Pricing
Ruby Academy · Lesson

Writing the Code

Organizing a gem.

Organizing a Gem's Code

Good gem code is easy to read, easy to require, and namespaced so it never collides with other libraries. The key tool is a top-level module that wraps everything.

  • One namespace module per gem.
  • One file per class, mirroring the namespace.
  • A small public surface.

The Namespace Module

Wrap all classes in a module named after your gem. This prevents your Client from clashing with someone else's Client.

module Greeter
  class Hello
    def message
      "Hi there"
    end
  end
end

puts Greeter::Hello.new.message

All lessons in this course

  1. Gem Structure
  2. Writing the Code
  3. Testing and Versioning
  4. Publishing to RubyGems
← Back to Ruby Academy