The Rails Console
The Rails Console is a quick, interactive way to test out Ruby expressions.
When I am setting out on a new programming task, or when I have to debug something that isn't working the way I think it ought to be, I usually begin by playing around in the Console. I find the quick feedback that it provides useful; and once I've found the Ruby methods that do what I need, I then go write them down for good in a
To launch a Console session,
cdinto the root folder of a Rails app. (I usually open a second command line tab to do this, while leaving my
rails serverrunning in the first one.)
- 2.Run the command
rails console, or
rails cfor short. This will launch a new (text-only) app.
- 3.You can then type any Ruby command you want at the slightly different looking prompt that appears (it will look something like
2.x.x :001 >or
- Sometimes you might get into a weird state with your commands (if, for example, you forget your closing
"on a string or
]on an array). To reset and get out of the weird state, press Ctrl+C. Then press the Up arrow to recover your last command, and fix the mistake.
- 4.When you are done testing out Ruby one-liners, type
exitand press return to shut down that program and return to the regular old command prompt. Or just close that command line tab.
That's it! After you've played around in this handy sandbox and discovered the Ruby you need to solve your problem, you are ready to head over to Atom and write it down permanently in a
.rbfile. Have fun!