Ruby On Rails Lesson
Strings and Methods
Our principal tool for learning Ruby will be the Rails console, a command-line tool for interacting with Rails applications first seen in Section 2.3.3. The console itself is built on top of interactive Ruby (irb), and thus has access to the full power of the Ruby language. (As we’ll see in Section 4.4.4, the console also has access to the Rails environment.) Start the console at the command line as follows:
By default, the consolestarts in a development environment, which is one of threeseparate environments defined by Rails (the others are test and production). This distinction won’t be important in this chapter, but we’ll learn more about environments in Section 7.1.1.
The console is a great learning tool, and you should feel free to explore—don’t worry, you (probably) won’t break anything. When using the console, type Ctrl-C if you get stuck, or Ctrl-D to exit the console altogether. Throughout the rest of this chapter, you might find it helpful to consult the Ruby API. It’s packed (perhaps even too packed) with information; for example, to learn more about Ruby strings you can look at the Ruby API entry for the String class.
Ruby comments start with the pound sign # (also called the ‘‘hash mark’’ or, more poetically, the ‘‘octothorpe’’) and extend to the end of the line. Ruby ignores comments, but they are useful for human readers (including, often, the original author!). In the code
the first line is a comment indicating the purpose of the subsequent function definition. You don’t ordinarily include comments in console sessions, but for instructional
purposes I’ll include some comments in what follows, like this:
If you follow along in this section typing or copying-and-pasting commands into your own console, you can of course omit the comments if you like; the console will ignore
them in any case.