Conventions Used in This Book

The following notation conventions are used throughout the book.

A # => precedes the output or results of executing a code snippet.

 p 2 + 2 ​# => 4

In order to keep code short, we’ll sometimes omit the p, puts or print in the book like this:

 2 + 2 ​# => 4

The Crystal Playground environment (see Working with Crystal Playground) shows the values and types of all expressions, so you don’t need to tell the program explicitly to print. Use p, puts, or print, however, to produce output in another editor or IDE.

Some code files contain lines that compile into an error. This is on purpose because errors can be very educational. Examining these error messages carefully gives you a good indication of what went wrong, and some messages even point to remedies. Comment out these lines if you want the file as a whole to compile and run.

Almost all of the errors happen while you’re compiling code, and we’ll indicate them with

 Error: message

after the offending line. A runtime error, an error that happens while the program is running, which is rare in Crystal, will appear as follows:

 Runtime error: message

If something needs to be invoked on the command-line terminal, you’ll see a $ sign preceding it in the text:

$ crystal build hello_world.cr

Some Ruby conventions are also used in Crystal:

  • A # sign indicates a comment.
  • In an expression such as Shape#perim, a # indicates the perim method on an instance of Shape.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.255.36