|
| 1 | +# Ruby Version Check (shared reference) |
| 2 | + |
| 3 | +**Purpose:** verify the active Ruby matches what a Rails project requires before installing gems or running generators. Mismatched Ruby is a common cause of confusing `bundle install` and `bin/rails` failures. |
| 4 | + |
| 5 | +**Used by:** any skill that touches a Rails project's gems, runs Rails generators, or runs `make test` / `make lint`. Reference this file from `<RAILS_DIR>` (the located Rails app directory) — all paths below are relative to it. |
| 6 | + |
| 7 | +## Step A: Determine the required version |
| 8 | + |
| 9 | +Check, in order: |
| 10 | + |
| 11 | +1. `<RAILS_DIR>/.ruby-version` (one line, e.g. `3.3.4`) |
| 12 | +2. `<RAILS_DIR>/Gemfile` for a `ruby "X.Y.Z"` line |
| 13 | +3. `<RAILS_DIR>/.tool-versions` for a `ruby X.Y.Z` line (asdf) |
| 14 | + |
| 15 | +Save the required version as `<REQUIRED_RUBY>`. If none of those files specify a Ruby version, skip the rest — there is no version to enforce. |
| 16 | + |
| 17 | +## Step B: Compare against the active Ruby |
| 18 | + |
| 19 | +```sh |
| 20 | +ruby -v |
| 21 | +``` |
| 22 | + |
| 23 | +If the major.minor.patch matches `<REQUIRED_RUBY>` → done. Continue with the calling skill. |
| 24 | + |
| 25 | +If it does not match → continue to Step C. |
| 26 | + |
| 27 | +## Step C: Ask which version manager the user uses |
| 28 | + |
| 29 | +> **Your active Ruby is `<active>` but the project requires `<REQUIRED_RUBY>`. Which Ruby version manager do you use? (rbenv / asdf / rvm / chruby / other)** |
| 30 | +
|
| 31 | +## Step D: Switch to the required version |
| 32 | + |
| 33 | +First check whether the version is installed; if not, install it. Then activate it. |
| 34 | + |
| 35 | +| Manager | Check installed | Install if missing | Activate | |
| 36 | +|---------|-----------------|--------------------|----------| |
| 37 | +| **rbenv** | `rbenv versions \| grep -q <REQUIRED_RUBY>` | `rbenv install <REQUIRED_RUBY>` | `rbenv local <REQUIRED_RUBY>` (run inside `<RAILS_DIR>`) | |
| 38 | +| **asdf** | `asdf list ruby \| grep -q <REQUIRED_RUBY>` | `asdf install ruby <REQUIRED_RUBY>` | `asdf local ruby <REQUIRED_RUBY>` (run inside `<RAILS_DIR>`) | |
| 39 | +| **rvm** | `rvm list strings \| grep -q <REQUIRED_RUBY>` | `rvm install <REQUIRED_RUBY>` | `rvm use <REQUIRED_RUBY>` | |
| 40 | +| **chruby** | `chruby \| grep -q <REQUIRED_RUBY>` | install via `ruby-install <REQUIRED_RUBY>` (tell user if `ruby-install` is missing) | `chruby <REQUIRED_RUBY>` | |
| 41 | +| **other / unsure** | — | — | Stop. Ask the user to switch manually, then confirm before continuing. | |
| 42 | + |
| 43 | +Notes: |
| 44 | + |
| 45 | +- `rbenv install` / `asdf install ruby` may take several minutes (compiling Ruby). Tell the user before running. |
| 46 | +- After activating, **re-run `ruby -v`** to confirm. If still mismatched (shell hasn't picked up the new version), stop and ask the user to open a new shell or `source` their rc file. |
| 47 | +- Do not run `sudo` for any of these — version managers are per-user. |
| 48 | + |
| 49 | +## Step E: Verify Bundler is available |
| 50 | + |
| 51 | +```sh |
| 52 | +bundle -v |
| 53 | +``` |
| 54 | + |
| 55 | +If `bundle` is missing → `gem install bundler`. Then continue with the calling skill. |
| 56 | + |
| 57 | +## Common pitfalls |
| 58 | + |
| 59 | +| Problem | Fix | |
| 60 | +|---------|-----| |
| 61 | +| `ruby -v` still old after `rbenv local` | Open a new shell, or run `eval "$(rbenv init -)"` in current shell | |
| 62 | +| `asdf install ruby <ver>` fails with build errors | User missing build deps (openssl, readline). Direct them to asdf-ruby README. | |
| 63 | +| Multiple version managers installed (e.g. rbenv + asdf) | Ask which one is authoritative; mixing causes silent shadowing | |
| 64 | +| `.ruby-version` and `Gemfile` disagree | `.ruby-version` wins for the version manager; Gemfile `ruby` line is enforced by Bundler. Ask user to reconcile. | |
0 commit comments