skip to content

In Ruby, which naming rules for a LibraryLoan class are enforced by the parser, and which are only snake_case and CamelCase conventions?

level: juniorimportance: must knowfreq 58%

answer

  1. only the first character is syntax
  2. capital initial means constant
  3. class/module name must be CONSTANT
  4. snake_case methods, CamelCase classes
  5. SCREAMING_SNAKE_CASE for other constants

basics

~20 s

The parser enforces only the first character: an uppercase initial makes a constant, so class and module names must be capitalised, while a lowercase or underscore initial makes a local or method. snake_case, CamelCase and SCREAMING_SNAKE_CASE are conventions.

solid answer

~40 s

Ruby's parser cares about the **first character**. A name that starts with an uppercase letter is a **constant**, which is why `class LibraryLoan` works and `class library_loan` is a `SyntaxError` (`class/module name must be CONSTANT`). A lowercase letter or underscore starts a local variable or method name, `@`, `@@` and `$` mark the other variable kinds, and only method names may end in `?`, `!` or `=`. The rest is **convention**: `snake_case` for methods, locals, instance variables and file names (`days_overdue`, `library_loan.rb`), `CamelCase` for classes and modules, and `SCREAMING_SNAKE_CASE` for other constants (`MAX_RENEWALS`). So `maxRenewals = 3` runs but reads as foreign code, while `Max_renewals = 3` quietly creates a constant rather than a local.

code

ruby · 20 lines
ruby
require "date"

class LibraryLoan               # CamelCase: a constant naming a class
  MAX_RENEWALS = 2              # SCREAMING_SNAKE_CASE constant

  attr_reader :due_date

  def initialize(due_date)
    @due_date = due_date        # snake_case after the sigil
    @renewals = 0
  end

  def overdue?(today = Date.today)
    days_overdue = (today - @due_date).to_i  # snake_case local
    days_overdue.positive?
  end
end

# class library_loan; end
# => SyntaxError: class/module name must be CONSTANT

go deeper

for a junior

Recall snake_case for methods and locals, CamelCase for classes and modules, SCREAMING_SNAKE_CASE for constants, and that a capital initial makes a constant.

for a middle

Separate what the parser enforces from style, and explain the SyntaxError for a lowercase class name and the constant created by a capitalised local.

for a senior

Spot naming that changes behaviour in review, such as accidental constants, and rely on tooling for the purely stylistic rules.

for a principal

Treat naming conventions as a shared contract that makes a codebase readable to any Rubyist, and decide which deviations, such as acronym style, the team standardises.

## Two layers: syntax and convention Ruby names carry meaning at two levels. The **parser** looks at a name's first character (and, for methods, its last) to decide what kind of thing it is. On top of that sits a **community convention** about how multiword names are spelled. Interviewers like to ask which is which, because breaking the first changes behaviour while breaking the second only makes code look foreign. ## What the parser enforces | A name that starts with | Is parsed as | Example | |---|---|---| | an uppercase letter | a constant (classes and modules are constants) | `LibraryLoan`, `MAX_RENEWALS` | | a lowercase letter or `_` | a local variable or a method call | `due_date`, `_unused` | | `@` | an instance variable | `@due_date` | | `@@` | a class variable | `@@loans_open` | | `$` | a global variable | `$stdout` | - `class` and `module` require a constant name, so `class library_loan` fails to parse with `class/module name must be CONSTANT`. - A local variable cannot start with a capital letter: `Due = 3` creates a constant instead, and inside a method the same line is a `SyntaxError` (`dynamic constant assignment`). - Only **method** names may end with `?`, `!` or `=`; `overdue? = true` is not a valid local variable. ## The conventions | Kind of name | Convention | In a library-loan class | |---|---|---| | methods and locals | `snake_case` | `days_overdue`, `renew!` | | instance variables | `snake_case` after the sigil | `@due_date` | | classes and modules | `CamelCase` (upper camel case) | `LibraryLoan`, `Circulation` | | other constants | `SCREAMING_SNAKE_CASE` | `MAX_RENEWALS`, `LOAN_DAYS` | | source files | `snake_case`, matching the class | `library_loan.rb` | | predicates | a trailing `?`, no `is_` prefix | `overdue?`, not `is_overdue` | None of these are checked by Ruby itself. `def dueDate` or `maxRenewals = 3` run fine; a linter or reviewer is what flags them. ## Edge cases worth knowing - **Methods may start with a capital letter.** `Kernel#Integer` and `Kernel#Array` are real methods: `Integer("42")` calls the method because of the parentheses and argument, while a bare `Integer` refers to the class constant. - **Acronyms** can be spelled `HTTPClient` or `HttpClient`; both are legal constants, and teams pick one style. - **A leading underscore** on a local, such as `_row`, signals that the value is intentionally unused, and Ruby's verbose "assigned but unused variable" warning skips such names. - **Non-ASCII letters** are allowed in identifiers, but ordinary Ruby code sticks to ASCII so that every keyboard can type the names. - **A capitalised local is a trap**: `Total = 0` at the top level quietly creates a constant, so reassigning it later only prints a warning. ## Naming the library-loan methods Beyond case, Ruby has conventions for what a name says: - **Readers are nouns**: `due_date`, not `get_due_date`. - **Setters end in `=`**: `due_date=`, not `set_due_date`, so callers write `loan.due_date = day`. - **Predicates end in `?`**: `overdue?`, `renewable?`. - **The dangerous twin ends in `!`**: `renew!` beside `renew`. - **Conversions start with `to_`**: `to_s`, `to_h`, following the core classes. A Rubyist reading `loan.get_due_date` or `loan.isOverdue()` immediately knows the author came from another language, which is why interviewers ask about names at all: they are the cheapest signal of fluency. ## How to answer in an interview 1. Say that only the first character is syntax: capital means constant. 2. Give the conventions with one example each, using the class at hand: `LibraryLoan`, `MAX_RENEWALS`, `due_date`, `overdue?`. 3. Mention one consequence of mixing them up, such as the `class library_loan` syntax error or `Max_renewals` becoming a constant. 4. Note that style beyond that is enforced by tooling, not by Ruby.

  • How can Integer("42") be a method call when Integer is also a class?
    Ruby resolves a capitalised name followed by parentheses or arguments as a method call, so `Integer("42")` calls `Kernel#Integer`, which converts its argument and raises on invalid input. A bare `Integer` with no arguments is looked up as a constant and returns the class. The same pattern exists for `Array`, `Float`, `String` and `Hash`.
  • What happens if you write Max_renewals = 3 instead of max_renewals = 3 inside a class body?
    Because it starts with a capital letter, `Max_renewals` is a constant, not a local variable. The line defines `LibraryLoan::Max_renewals`, visible to the whole class and from outside as `LibraryLoan::Max_renewals`, and assigning it again later prints an 'already initialized constant' warning. Inside a method body, the same line would not parse at all.

saying these in an interview costs you the question

  • Ruby raises an error when a method name uses camelCase
  • A local variable can start with a capital letter if it is assigned inside a method
  • Class names are just a convention; class library_loan works too
  • Method names can never begin with an uppercase letter
  • Predicate methods should be named is_overdue to follow Ruby style