In a plain Ruby gem that requires only active_support, why does 3.days raise NoMethodError, and how should the gem load just the extensions it uses?
answer
- require "active_support" loads the minimum
- Rails apps require active_support/all
- config.active_support.bare opts out
- one file per extension under core_ext
- a Rails test suite hides missing requires
basics
~20 srequire "active_support" loads only what the framework itself needs, not most core extensions; a Rails app gets them because boot requires active_support/all. A gem should require "active_support" and then each file it uses, such as active_support/core_ext/integer/time.
solid answer
~40 s`require "active_support"` keeps the footprint small: it sets up autoloads and loads what Active Support needs for itself, but not `Integer#days`, `blank?` or `to_sentence`. Inside a Rails app nobody notices, because boot runs `require "active_support/all"` unless `config.active_support.bare` is true. A gem or script should cherry-pick: `require "active_support"` and then `require "active_support/core_ext/integer/time"` for `3.days`, `.../object/blank` for `blank?`, `.../object/inclusion` for `in?`, `.../module/delegation` for `delegate`, or a whole class with `active_support/core_ext/string`. That keeps memory and boot time down in non-Rails hosts and keeps the gem from silently depending on extensions it never loaded. Run the gem's tests outside a Rails app, because inside one everything is already loaded and a missing require stays invisible.
code
ruby · 9 linesrequire "active_support"
3.days # NoMethodError: undefined method 'days' for an instance of Integer
require "active_support/core_ext/integer/time"
3.days # => 3 days
2.weeks.ago # => a Time two weeks back
require "active_support/core_ext/object/inclusion"
:strict.in?(%i[strict relaxed]) # => truego deeper
Recall that blank?, try and 3.days come from Active Support, not Ruby, and exist in a Rails app because Rails loads them all.
Explain the entry points: active_support alone, one core_ext file, a class group, core_ext and active_support/all, and what config.active_support.bare does.
Show that a library should cherry-pick its extensions and test outside Rails, because a full Rails load hides missing requires.
Weigh whether a shared library should depend on Active Support at all, given its effect on host apps and on non-Rails consumers.
## Rails and Ruby are not the same language surface Many methods Rails developers use daily are not Ruby at all. `3.days.ago`, `blank?`, `present?`, `try`, `in?`, `to_sentence`, `delegate` and `"event".pluralize` are all **core extensions** that Active Support adds by reopening Ruby's classes. Inside a Rails app they simply exist, so it is easy to forget where they come from until the same code runs in a plain Ruby script, a Rake task outside the app, or a gem used by a Sinatra service, and fails with `NoMethodError`. ## What each entry point loads Active Support is split into small files precisely so that a program can load only what it uses: | Require | What you get | |---|---| | `require "active_support"` | the framework's own minimum and its autoloads, which pulls in a few extensions it uses internally (`Module#delegate` among them) but not `blank?`, `try` or `3.days` | | `require "active_support/core_ext/object/blank"` | one extension: `blank?`, `present?`, `presence` | | `require "active_support/core_ext/string"` | every `String` extension | | `require "active_support/core_ext"` | every core extension | | `require "active_support/all"` | all of Active Support, with some parts autoloaded on first use | The guide's pattern is always `require "active_support"` first, then the specific file. Each extension file requires what it depends on: `active_support/core_ext/integer/time` pulls in `ActiveSupport::Duration` and the `Numeric` time methods, so `3.days` and `1.month` both work after that single line. A Rails application takes the opposite default. During boot, `Rails::Application::Bootstrap` runs `require "active_support/all"` unless `config.active_support.bare` is true. With `bare` on, the app gets only what the frameworks cherry-pick for themselves, and it can cherry-pick further at any granularity. ## A gem done right A gem that computes RSVP deadlines for several frameworks should declare `activesupport` in its gemspec and load exactly what it calls: ```ruby # lib/rsvp_deadlines.rb require "active_support" require "active_support/core_ext/integer/time" # 2.weeks, 1.month require "active_support/core_ext/object/blank" # blank?, presence require "active_support/core_ext/object/inclusion" # in? module RsvpDeadlines def self.due_at(starts_at, policy) raise ArgumentError, "unknown policy" unless policy.in?(%i[strict relaxed]) starts_at - (policy == :strict ? 3.weeks : 1.week) end end ``` Why bother instead of `require "active_support/all"`: - **Footprint.** A CLI or a small Rack service pays memory and boot time for every extension loaded. - **Honesty.** The gem's dependency on Active Support is visible and limited to what it really uses. - **Host behaviour.** Loading all extensions changes classes the host app owns too, which is a decision the host, not a library, should make. ## The invisible missing require The classic failure: a gem's own test suite runs inside a Rails app (or with `active_support/all` loaded by the test helper), so every extension exists and the tests pass. The first user who loads the gem from a plain Ruby program hits `NoMethodError` on `in?` or `blank?`. Two habits prevent it: 1. Run the gem's test suite with only the gem's own requires loaded. 2. Keep the test helper from requiring more of Active Support than the library does. ## Diagnosing a missing extension When a gem or script fails with `NoMethodError` on a method that "works in Rails": 1. Read the receiver in the message (`for an instance of Integer`) and the method name. 2. Find the defining file: the core extensions guide lists it, or `3.method(:days).source_location` in a Rails console prints it. 3. Require that path after `require "active_support"`, and rerun the suite in plain Ruby to catch the next one. ## Telling Rails from Ruby in an interview Interviewers like to ask which side a method lives on. A few useful anchors: - `blank?`, `present?`, `try`, `in?`, `to_sentence`, `3.days`, `delegate` and `String#pluralize`: Active Support. - `&.`, `nil?`, `empty?`, `then`, `Array#sum` and `Hash#except`: Ruby. Active Support 8.1 only adds the bang form `Hash#except!`, now that Ruby has `except` itself. - The set moves over time. In Rails 8.1, `Benchmark.ms` was removed from Active Support (the release notes point to the `benchmark` gem), so `active_support/core_ext/benchmark` no longer defines anything, and `String#mb_chars` is deprecated, since Ruby's own `String` methods already handle Unicode case mapping. Knowing which file defines a method is also how you read a stack trace: a frame under `active_support/core_ext/...` means a Rails extension, not Ruby core, is involved.
- What does `config.active_support.bare = true` change in a Rails app?Boot skips `require "active_support/all"`, so the app gets only the extensions the Rails frameworks cherry-pick for themselves. Application code that relies on anything else must require it explicitly. It is rarely used, mainly to trim memory or to find hidden dependencies on extensions.
- How would you find which file to require for a given extension method?The Active Support Core Extensions guide notes the defining file for every method, for example `active_support/core_ext/object/inclusion.rb` for `in?`. In a console, `3.method(:days).source_location` also points at the file. Require the path without `.rb`, after `require "active_support"`.
- require "active_support" loads every core extension, so 3.days always works after it.
- blank? and present? are part of Ruby's standard library.
- A gem should require active_support/all so all its users get the same behaviour.
- If the gem's tests pass inside a Rails app, its requires are complete.
- Hash#except comes from Active Support, so plain Ruby cannot call it.
Assumes Rails 8.1: Benchmark.ms was removed from Active Support and String#mb_chars was deprecated in 8.1, so the extension set differs from 7.x.