Starting the Rails console

Free preview 2 min to read

A second way to run Rails code

So far this arc has been about bin/rails server, Puma and a browser sending HTTP requests. There's another way to run Rails code that skips the browser, the server and HTTP: the Rails console.

bin/rails console

Running it in this project prints:

Loading development environment (Rails 8.1.4)
newbie-on-rails(dev):001>

The prompt shows the application and environment, then numbers each line you type. It waits for Ruby, one line at a time. bin/rails c is a shorter alias.

Not a new language, IRB with this app loaded

The prompt is IRB, Interactive Ruby, the shell that ships with Ruby and runs on its own as irb. bin/rails console starts that same IRB with one difference, covered in lesson 2: every class this app defines is already loaded.

No server involved, on purpose

No Puma runs in the background and there's no request/response cycle. Nothing comes from a browser: no URL, no route, no HTTP verb. Code you type runs immediately in the same process, and its return value prints below it.

Leaving the console

exit or Ctrl+D ends the session and returns you to the shell. It's like Ctrl+C stopping bin/rails server in the previous course, though nothing needs interrupting: the process finishes when told to.

Key takeaways

  • bin/rails console (or bin/rails c) starts an interactive Ruby session with the app loaded.
  • The console is IRB, Ruby's own interactive shell.
  • No HTTP, browser or route is involved; code runs immediately in the same process.
  • exit or Ctrl+D ends the session.
Lesson 1 of 7 · free

Keep going with lesson 2

Plain Ruby, with your app already loaded

Plain Ruby works as usual in the console, and every class the app defines is loaded without require.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...