Home / Womply Ruby Style Guide

Ruby Naming Conventions: Methods, Classes, Predicates and Constants

The documentation that used to be at this address is no longer published, and its source was never released under an open license, so it is not reproduced here. This page is an independent guide to the same topic, written for this archive. It is not the project's documentation and is not affiliated with its authors.

Names are the documentation you cannot skip. Ruby has strong community conventions for naming, and following them means other Rubyists can read your code without a glossary.

Casing

KindConventionExample
Methods and variablessnake_caseorder_total
Classes and modulesCamelCaseInvoiceMailer
ConstantsSCREAMING_SNAKE_CASEMAX_RETRIES
Files and directoriessnake_caseinvoice_mailer.rb

Keep acronyms upper case in class names, for example HTTPClient or XMLParser, unless your autoloader expects otherwise, and then be consistent.

Predicate methods

A method that returns a boolean ends with a question mark and does not start with is_.

# Avoid
def is_active
  status == "active"
end

# Prefer
def active?
  status == "active"
end

Bang methods

A trailing exclamation mark signals that a method is the more dangerous version of a safer method with the same name. It usually mutates the receiver or raises instead of returning false. Only use a bang when a non bang counterpart exists.

name.strip     # returns a new string
name.strip!    # modifies name in place

user.save      # returns true or false
user.save!     # raises on failure

Setters and getters

Do not prefix readers with get_ or writers with set_. Ruby already has attr_reader and name= style writers.

class Account
  attr_reader :balance

  def balance=(amount)
    raise ArgumentError, "negative balance" if amount.negative?

    @balance = amount
  end
end

Choosing good names

More on Womply Ruby Style Guide

Explore the archive