Code Snippets

We have provided code snippets throughout the book that show how RSpec is used in real-world situations. Most of these examples are intended for you to follow along with on your computer, particularly those in Part I and Part II.

A typical snippet will contain one or more lines of Ruby code meant for you to type into your text editor so that you can run them later. Here is an example:

00-introduction/01/type_me_in.rb
 puts ​"You can type me in; it's okay!"

We’ll show each code file a few lines at a time. If you need more context for any given snippet, you can click the filename banner (in the eBook) or open the book’s source code (linked at the end of this chapter) to view the entire file at once.

Some code examples have no banner; these typically represent a session at your terminal, either in interactive Ruby (IRB) or in a shell like Bash. For IRB snippets, you’ll run the irb terminal command and then type in just the parts after the green >> prompt:

 >>​ ​%w[Type in just the bit after the prompt]​.join(​' '​)
 => "Type in just the bit after the prompt"

We’ll represent shell sessions with a green $ prompt instead. As with IRB sessions, you won’t type in the prompt or the output lines, just the commands after the prompt:

 $ ​​echo 'RSpec is great!'
 RSpec is great!

Later on in the book, we sometimes show isolated snippets from a larger project; these are not meant for you to run on your computer. If you’re interested in running them on your own, you can download all the project files from the book’s source code repository.

Most chapters have a “Your Turn” section with exercises for you to try. Don’t skip these! Practicing on your own will ensure that each chapter builds on the skills you’ve honed over the course of the book.