In a Rails app, what does `inflect.acronym "HTTP"` change for a file named lib/archive_http_client.rb, and what breaks if you add it late?
answer
- camelize keeps the acronym's capitals
- ArchiveHttpClient becomes ArchiveHTTPClient
- the autoloader camelizes file names
- Zeitwerk::NameError on the old constant
- acronyms must be delimited words
basics
~10 sWith inflect.acronym "HTTP", Rails camelizes archive_http_client to ArchiveHTTPClient instead of ArchiveHttpClient, so the file must define that constant; adding it after code uses ArchiveHttpClient breaks loading until every reference is renamed.
solid answer
~40 s`inflect.acronym "HTTP"` tells Active Support's inflector that `http`, as a delimited word, camelizes to `HTTP` and that `HTTP` underscores back to `http`. Rails' autoloader turns file names into constant names with that same `camelize`, so `lib/archive_http_client.rb` is now expected to define `ArchiveHTTPClient`. In an app where the class was written as `ArchiveHttpClient`, adding the acronym later makes the file fail its contract: loading it raises `Zeitwerk::NameError` ("expected file ... to define constant ArchiveHTTPClient, but didn't"), and production fails at boot because it eager loads. The change is global: route namespaces, generators and `titleize` all follow it, and it only applies where the acronym is a whole word, so `https` still camelizes to `Https` unless `HTTPS` is also declared. Decide acronyms early, rename every reference in the same change, and restart.
code
ruby · 11 lines# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym "HTTP"
end
# lib/archive_http_client.rb must now define:
class ArchiveHTTPClient
def initialize(base_url)
@base_url = base_url
end
endgo deeper
Recall that inflect.acronym makes camelize keep capitals, so http_client becomes HTTPClient, and that it lives in config/initializers/inflections.rb.
Explain why the autoloader cares: it camelizes file names with the same inflector, so the acronym changes which constant each file must define.
Treat an acronym as a global rename: find affected constants and route namespaces, rename them together, check eager loading and deploy with a restart.
Choose a naming style for acronyms once per codebase and enforce it, rather than letting a late tidy-up become a breaking change.
## What an acronym rule does Active Support's inflector converts between `snake_case` names and `CamelCase` constants. By default `"archive_http_client".camelize` returns `"ArchiveHttpClient"`: every word gets one capital letter. Teams that prefer `HTTP`, `API` or `HTML` in constant names declare **acronyms** in `config/initializers/inflections.rb`: ```ruby ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym "HTTP" end ``` After that: - `"archive_http_client".camelize` returns `"ArchiveHTTPClient"`; - `"ArchiveHTTPClient".underscore` returns `"archive_http_client"`; - `titleize` and `humanize` keep the capitals as well. The acronym is given exactly as it should appear in a camelized string, and it must start with a capital letter. ## Why it reaches the autoloader Rails configures its autoloader to derive the constant a file should define with `String#camelize`. So a global acronym rule changes what **every file name** is expected to define: | File | Without the acronym | With `acronym "HTTP"` | |---|---|---| | `lib/archive_http_client.rb` | `ArchiveHttpClient` | `ArchiveHTTPClient` | | `app/models/http_log.rb` | `HttpLog` | `HTTPLog` | In a Rails 8.1 app, `config.autoload_lib(ignore: %w[assets tasks])` makes `lib/` autoloaded, so files there are covered too. How the autoloader can be told about one-off names without a global rule is a separate topic; the point here is that acronyms are **global**. ## What breaks when it is added late Suppose the genealogy app has used `ArchiveHttpClient` for a year, and someone adds the acronym to tidy up a new `HTTPLog` model: 1. The loader now expects `lib/archive_http_client.rb` to define `ArchiveHTTPClient`. Loading the file raises **`Zeitwerk::NameError`**: "expected file .../archive_http_client.rb to define constant ArchiveHTTPClient, but didn't". 2. Code that still says `ArchiveHttpClient` fails with an uninitialized-constant error, because no file maps to that name any more. 3. In production, eager loading runs at boot, so the app fails to start instead of failing on the first request that touches the class. 4. Route namespaces follow too: with `acronym "API"`, `namespace :api` resolves controllers under `API::`, so existing `Api::...` controllers stop matching. The fix is mechanical but must be complete: rename every constant that contains the acronym in the same commit, run the app's eager-load check (`bin/rails zeitwerk:check`) and restart. ## The edges of the rule - **Only delimited words**: with `acronym "HTTP"`, `"my_http_delimited".camelize` gives `"MyHTTPDelimited"`, but `"https".camelize` is still `"Https"`, and `"HTTPS".underscore` becomes `"http_s"`. Declare `HTTPS` separately if you use it. - **Plurals are not covered**: `"api".pluralize.camelize` gives `"Apis"` even with `acronym "API"`; you would also declare `"APIs"`. - **Any odd capitalisation works**: the rule accepts words like `RESTful` or `McDonald`, which is why the generated initializer shows `inflect.acronym "RESTful"` as its example. ## Choosing whether to use it - Use acronyms from the start of a project if the team prefers `HTTPClient` style, and apply them to every constant. - Leave them out if the codebase already uses `HttpClient` style; consistency matters more than either style. - Never add one as a drive-by change: it is a global rename of expected constants. ## A narrower alternative If only one or two files need special capitalisation, Rails also lets you give the autoloader per-file overrides, for example mapping `"archive_http_client"` to `"ArchiveHTTPClient"`, without changing how Active Support inflects strings elsewhere. That keeps routes, generators and `titleize` untouched. It is the autoloading configuration's job rather than the inflections initializer's, and the trade-off is visibility: a global acronym is easy to find in one initializer, while scattered overrides are easy to forget. Teams usually pick one approach and document it next to their naming conventions.
- With `inflect.acronym "HTTP"` declared, why does `"https".camelize` still return `"Https"`?An acronym is only recognised as a delimited unit, a whole word between underscores or at the edges. In `https` the letters `http` are part of a longer word, so the generic rule capitalises it once. Declaring `inflect.acronym "HTTPS"` makes `https` camelize to `HTTPS`.
- How would you find every file an acronym change breaks before deploying?Run `bin/rails zeitwerk:check`, which eager loads the app and reports files that do not define the constant their name implies. Running the test suite with eager loading enabled, as CI does in a generated app, catches the same class of error before production does.
- An acronym rule only changes titleize output in views.
- Rails autoloading ignores Active Support inflections when mapping file names.
- Once HTTP is declared, https camelizes to HTTPS as well.
- Adding an acronym is a safe, local change that needs no renames.
- Acronym rules take effect without restarting the server.