Rails Under the Microscope: What 67 Remote AST Tools Found Inside the Ruby Web Framework
Rails framework analysis with 3,478 Ruby files, Active Record queries, component packages, and test/error search results; subsystem line subtotals are withdrawn due to a discrepancy.
On this page · 5 sections
First published on 2026-10-04. The measurements reported in this article are historical and have not been re-run for this revision.
Few software frameworks have exerted as profound an influence on modern software engineering as Ruby on Rails. Created by David Heinemeier Hansson in 2004, Rails established the paradigm of convention over configuration, RESTful routing, migration-driven database schemas, and object-relational mapping patterns that inspired Django, Laravel, Phoenix, and ASP.NET MVC.
Behind its approachable domain-specific language (DSL) lies a sophisticated multi-gem monorepo: an immutable relational query builder powered by Arel AST nodes, an onion-architecture action dispatch pipeline, an asynchronous job runner, and a full-stack engine system (Railties).
To analyze how modern Rails (8.1 development branch) structures its component packages, manages cross-gem boundaries, and validates runtime invariants, we deployed selected operations from prod-code’s 67-tool suite against a checkout mirrored to a remote 32-core cluster node (node-b:9400).
$ git ls-files '*.rb' | wc -l
3478
$ git ls-files -z '*.rb' | xargs -0 wc -l | tail -n 1
573455 total
$ git ls-files | awk -F. '{if (NF>1) print $NF}' | sort | uniq -c | sort -nr | head -n 6
3478 rb
514 erb
207 yml
165 tt
129 md
56 js
The repository-wide command printed 573,455 lines across 3,478 Ruby files. The file subtotals below add to 3,478, but the line subtotals add to 553,455, leaving a 20,000-line discrepancy. The source revision was not pinned, so the line total and subsystem line breakdown are withdrawn until remeasured against a fixed tree; only the file counts are retained:
- Active Record (
activerecord/): 1,176 files. The object-relational mapping engine, database adapters (PostgreSQL, MySQL, SQLite, MariaDB), connection pools, and migration runners. - Action Pack & Action View (
actionpack/,actionview/): 561 files. Routing dispatchers, controller lifecycles, cookies/session managers, and view template builders. - Active Support (
activesupport/): 531 files. Core Ruby extensions, structured notifications, isolated concerns, and thread-safe concurrency primitives. - Railties (
railties/): 352 files. Rails engine loaders, CLI generators, and framework boot configuration. - Peripheral Subsystems (
activejob/,actioncable/,activestorage/,actionmailbox/,actiontext/): 858 files.
Subsystem Architecture: Decoupled Multi-Gem DAG
Rails organizes its framework components into autonomous gems within the repository:
$ prod-code dependencies --scope modules
⚡ prod-code Architecture & Dependency Graph Report
────────────────────────────────────────────────────
Scope: modules | Nodes: 57 | Dependencies: 69
✓ Zero circular dependencies detected. Architecture graph is a clean DAG.
As reported, the extracted module graph has 57 nodes and 69 dependencies and no cycle, so that graph is a Directed Acyclic Graph (DAG). The excerpt shows only this summary: it lists no edges and does not say which files or references the extraction covered.
Correction, 2026-10-27. As published on 2026-10-04, this paragraph said the architecture forms a pure DAG, that
ActiveSupporthas zero knowledge of higher-level database or controller abstractions, and thatActiveSupportorActiveRecordcan be consumed as completely independent gems. The cited output is a summary of an extracted module graph without its edges or coverage; it establishes only that the graph it extracted has no cycle. The claims about whatActiveSupportknows of other layers and about independent use of the gems are withdrawn.
The Active Record Query Engine: Immutable Relations and Lazy Loading
The architectural centerpiece of Rails’s developer experience is ActiveRecord::Relation (activerecord/lib/active_record/relation.rb, 1,576 lines).
In Active Record, scoping methods such as where, joins, select, and order do not execute SQL immediately. Instead, the public scoping methods usually return a new Relation built from a copy of the current one, with the new conditions added for later compilation through Arel. This is not true of every call: in Rails v8.1.0, where with a blank condition returns the same relation, and the bang variants such as where! modify the receiver.
Correction, 2026-10-27. As published on 2026-10-04, this paragraph said each scoping method invocation clones the current
Relation. In Rails v8.1.0query_methods.rb,wherereturnsselffor a single blank argument andwhere!adds to the receiver’s where clause and returns it; the unconditional “each invocation clones” claim is withdrawn and narrowed to the public methods that spawn a copy.
Execution is deferred until records are explicitly needed:
def load(&block)
if !loaded? || scheduled?
@records = exec_queries(&block)
@loaded = true
end
self
end
Inside exec_queries:
- Query Cache Checking: Active Record checks whether identical query results exist in the per-request query cache before hitting database connections.
- Arel AST Compilation: The relation compiles its chained predicates into an optimized SQL AST matching the active connection’s database dialect.
- Record Instantiation: Tabular SQL rows are hydrated into domain model instances with dirty-tracking attributes and cast types.
- Association Preloading: If eager loading was declared (
includes,preload), Active Record resolves child associations without triggering N+1 query waterfalls. - Scheduled Results: Modern Rails supports asynchronous query execution (
load_async). When the application calls it,load_asynccan schedule the query in the background if async execution is enabled for the connection and the relation is not already loaded; otherwise it loads synchronously or returns what is already loaded.exec_queriesdoes not schedule anything; it consumes the future when one exists.
Correction, 2026-10-27. As published on 2026-10-04, this list placed asynchronous query scheduling inside
exec_queries, before view rendering starts. In Rails v8.1.0relation.rb,load_asyncmay start the background query and store it as@future_result, subject to the conditions in the list above (async enabled, relation not yet loaded, transaction state), andexec_queriestakes that result when there is one; when this happens relative to rendering depends on where the application callsload_async. The placement insideexec_queriesis withdrawn.
Clone Analysis and Header Standardization
We executed prod-code duplicates to detect structural duplication:
$ prod-code duplicates --min-lines 6 --max-groups 5
⚡ prod-code Clone & Duplication Harvester Report
────────────────────────────────────────────────────
Files Scanned: 3532 | Lines: 586243 | Clone Groups: 5 | Duplication: 1.2%
Discovered Clone Groups:
[Clone Group #1946] 6 lines | 302 occurrences (Type-2 (Parameterized))
• Occurrence 1: actionpack/test/dispatch/cookies_test.rb:1-6
• Occurrence 2: actionpack/test/dispatch/system_testing/driver_test.rb:1-6
• Occurrence 3: actionpack/test/dispatch/system_testing/screenshot_helper_test.rb:1-6
• Occurrence 4: actionpack/test/dispatch/routing_test.rb:1-6
• Occurrence 5: actionpack/test/dispatch/response_test.rb:1-6
Preview:
│ # frozen_string_literal: true
│
│ require "abstract_unit"
│ require "active_support/key_generator"
Duplication across 586,243 lines is low at 1.2%. The displayed group, with 302 occurrences, reflects Ruby’s # frozen_string_literal: true pragma and the standard require "abstract_unit" test bootstrapping block; the other four groups are not shown.
The shown clone report is capped at five groups (--max-groups 5) and displays one of them, made of test-file headers; it does not show how the library code in activerecord and actionpack reuses behaviour.
Correction, 2026-10-27. As published on 2026-10-04, this paragraph said core libraries across
activerecordandactionpackdemonstrate strong modular reuse, usingActiveSupport::Concernto compose locking, callbacks and validations without duplicating method definitions. The cited output is a clone report capped at five groups whose only displayed group is a six-line test header; it says nothing about concerns or method definitions in library code, and the claim is withdrawn.
Test and Error-Handling Matching Lines (Not Construct Counts)
Rails maintains its stability across diverse databases and Ruby runtimes through extensive test verification:
$ grep -rnE "(assert|assert_equal|assert_not|assert_raises)" */test/ | wc -l
60253
$ grep -rnE "raise " */lib/ | wc -l
1365
These commands count matching output lines, not assertion calls or exception egress sites. The assertion pattern can match comments and helpers, and it does not distinguish multiple calls on one line; the raise pattern can miss raise(...) and bare re-raises while matching comments. Treat 60,253 and 1,365 only as matching-line counts, not exact construct or error-boundary totals.
Remote AST Operations on Cluster Nodes
To verify semantic manipulation on Ruby codebases, we tested prod-code extract-function on inflections string manipulation logic in activesupport/lib/active_support/core_ext/string/inflections.rb.
The article reports that the remote cluster node extracted the token normalization logic and reported no new analyzer diagnostics, in 13 milliseconds. No command, patch or analyzer output was published, so this is an unverified author report. The extraction ran on the node rather than the laptop; no local battery or memory measurement was published.
Correction, 2026-10-27. As published on 2026-10-04, this paragraph said the node analyzed dynamic metaprogramming signatures and confirmed zero analyzer regressions in 13 milliseconds without draining local battery or memory. The article shows no invocation, diff or diagnostics for the extraction and no local resource measurement. The 13 ms figure is kept as reported; the metaprogramming analysis, the confirmation of zero regressions and the claim about local battery and memory are withdrawn.
Rails illustrates how an expressive dynamic language can support a large framework, 3,478 Ruby files in the reported count, when backed by separate gem boundaries, lazy query compilation, and an extensive test suite (60,253 assertion-pattern matching lines, as reported, which is not a count of assertions).
Editorial note, 2026-10-11: the host names, addresses and home-directory paths of our build nodes in this post were replaced with the placeholders node-a, node-b and <user>. No other part of the quoted output was changed.
Cite this article
Alexander Panasenko (2026-10-04). Rails Under the Microscope: What 67 Remote AST Tools Found Inside the Ruby Web Framework. https://prod.codes/blog/rails-under-the-microscope-67-ast-tools/