# ruby/rbs architecture

> Architecture diagram of the GitHub repository ruby/rbs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ruby/rbs
- Repository: https://github.com/ruby/rbs

## Overview

##### Main components
- CLI Interface: This is represented primarily by the executables in the “bin” and “exe” directories. The README shows commands like “rbs version”, “rbs prototype”, etc. The CLI takes user commands, dispatches them, and interacts with the underlying library.
- Core Library/Business Logic: The “lib” directory is the heart of the project. It includes modules such as RBS::Environment, RBS::DefinitionBuilder, RBS::Parser, RBS::Resolver, and others that handle environment loading, definition building, method type parsing, etc.
- Signature Repositories: There are multiple directories holding signature files:
– “sig”: Contains user-contributed or community signatures.
– “core”: Provides signature files for Ruby’s core classes.
– “stdlib”: Contains signature files for the Ruby standard library.
- C Extensions: The “ext” directory holds components such as the lexer and parser implemented in C (e.g., lexer.c, parser.c). These are used for performance-critical operations.
- Tests & Benchmarks: The “test” and “benchmark” directories ensure reliability and performance. They interact with the core library by verifying the parsing, environment resolution, and CLI functionalities.
- Documentation & Configuration: The “docs” folder (along with relevant config files like Gemfile, Rakefile, CI configurations in .github) helps in building, testing, and deploying the project.

##### Relationships and interactions between the components
- The CLI layer (from “bin” and “exe”) is the entry point for the user. When a command like “rbs prototype” is executed, the CLI parses input, calls functions provided by the core library.
- The core library (under “lib”) handles actions:
– It uses the EnvironmentLoader (and RBS::Environment) to load type signature data from the “sig”, “core”, and “stdlib” directories.
– It then passes these definitions to the DefinitionBuilder to construct concrete runtime definitions of types.
– The Parser component (which may tap into the C extension modules) interprets Ruby code or RBI files.
– The Resolver & Factory modules assemble and check type dependencies.
- The C extensions in “ext” provide performance optimized implementations for lexing and parsing tasks — these integrate with the Ruby core library.
- Testing and benchmarking components interact with every layer ensuring the correctness of the CLI, library, and C extensions.

##### Important architectural patterns and design principles
- Layered Architecture: Clearly separating the CLI presentation layer from the core business logic and type signature repositories.
- Modular Design: Each component (environment loader, parser, definition builder, resolver, etc.) is implemented as a self-contained module inside “lib” and referenced in the CLI.
- Extensibility: The use of separate signature directories (“sig”, “core”, “stdlib”) underlines the extensible nature of the tool. It supports overrides and additions without modifying the core logic.
- Use of C Extensions: For performance-critical operations, showing clear boundaries between Ruby and C layers.
- Clear separation between declaration (what is defined in the RBS files) and definition (the fully resolved type information built at runtime).

##### Relevant technologies and frameworks
- Ruby is the primary language.
- Bundler and Rake are used for dependency management and task automation.
- CI/CD is set up through GitHub workflows.
- The use of C for extensions enhances performance (as seen in the “ext” directory).
- Testing frameworks (likely Minitest given the test structure) are used for unit and integration testing.

##### Instructing the principal software engineer
- Subdivide this into a CLI layer (bins/exes), Core Library (modules in lib), Signature Databases (sig, core, stdlib), C Extensions (ext), and Testing/Documentation.
- Core Library to the Signature repositories to demonstrate how type information is loaded and resolved.
- Additionally, show external integrations like Bundler, CI (GitHub workflows), and documentation as supporting components.
- Use additional notes or callouts to mention patterns like “Layered Architecture”, “Modularity”, “Separation of Definition & Declaration”, or “Performance via C Extensions”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "RBS System"
        subgraph "CLI Layer"
            CLI_bin["CLI (bin)"]
            CLI_exe["CLI (exe)"]
        end

        subgraph "Core Library"
            LIB["Core Library (lib)"]
        end

        subgraph "Signature Repositories"
            SIG["Signatures (sig)"]
            CORE_SIG["Core Signatures (core)"]
            STD_SIG["StdLib Signatures (stdlib)"]
        end

        subgraph "C Extensions"
            EXT["C Extensions (ext/rbs_extension)"]
        end

        subgraph "Tests & Benchmarks"
            TEST["Tests (test)"]
            BENCH["Benchmarks (benchmark)"]
        end

        subgraph "Documentation & Configuration"
            DOCS["Documentation (docs)"]
            GITHUB["CI/Config (.github)"]
            GEMFILE["Gemfile"]
            RAKEFILE["Rakefile"]
        end
    end

    %% Relationships
    CLI_bin -->|"CallsIntoCoreAPI"| LIB
    CLI_exe -->|"CallsIntoCoreAPI"| LIB

    LIB -->|"LoadsTypeSignatures"| SIG
    LIB -->|"LoadsTypeSignatures"| CORE_SIG
    LIB -->|"LoadsTypeSignatures"| STD_SIG

    LIB <-->|"DelegatesLexingParsing"| EXT

    TEST -->|"ValidatesCLI"| CLI_bin
    TEST -->|"ValidatesCLI"| CLI_exe
    TEST -->|"ValidatesLibrary"| LIB
    TEST -->|"ValidatesExtensions"| EXT
    BENCH -->|"BenchmarksLibrary"| LIB
    BENCH -->|"BenchmarksExtensions"| EXT

    DOCS ---|"SupportsBuildDeploy"| LIB
    DOCS --- GITHUB
    GEMFILE --- RAKEFILE

    %% Click Events
    click CLI_bin "https://github.com/ruby/rbs/tree/master/bin/"
    click CLI_exe "https://github.com/ruby/rbs/tree/master/exe/"
    click LIB "https://github.com/ruby/rbs/tree/master/lib/"
    click SIG "https://github.com/ruby/rbs/tree/master/sig/"
    click CORE_SIG "https://github.com/ruby/rbs/tree/master/core/"
    click STD_SIG "https://github.com/ruby/rbs/tree/master/stdlib/"
    click EXT "https://github.com/ruby/rbs/tree/master/ext/rbs_extension/"
    click TEST "https://github.com/ruby/rbs/tree/master/test/"
    click BENCH "https://github.com/ruby/rbs/tree/master/benchmark/"
    click DOCS "https://github.com/ruby/rbs/tree/master/docs/"
    click GITHUB "https://github.com/ruby/rbs/tree/master/.github/"
    click GEMFILE "https://github.com/ruby/rbs/tree/master/Gemfile"
    click RAKEFILE "https://github.com/ruby/rbs/tree/master/Rakefile"

    %% Styles
    classDef cli fill:#cce5ff,stroke:#000,stroke-width:2px;
    classDef lib fill:#d4edda,stroke:#000,stroke-width:2px;
    classDef sig fill:#fff3cd,stroke:#000,stroke-width:2px;
    classDef ext fill:#f8d7da,stroke:#000,stroke-width:2px;
    classDef test fill:#d1c4e9,stroke:#000,stroke-width:2px;
    classDef docs fill:#e2e3e5,stroke:#000,stroke-width:2px;

    class CLI_bin,CLI_exe cli;
    class LIB lib;
    class SIG,CORE_SIG,STD_SIG sig;
    class EXT ext;
    class TEST,BENCH test;
    class DOCS,GITHUB,GEMFILE,RAKEFILE docs;
```
