Information Technology Reference
In-Depth Information
Similarly, Ruby follows the Principle of Least Surprise (also called the Principle of Least
Astonishment), which aims to minimize confusion for both new and experienced users. The
principle encourages consistency, common design patterns, and reusable code. The Ruby
principles of simplicity and ease of use are echoed throughout Chef as well.
There is much more to the Ruby programming language, but that knowledge is not required
to use Chef. For a more detailed explanation of Ruby and the Ruby programming language,
check out Programming Ruby 1.9 & 2.0 (4th edition): The Pragmatic Programmers' Guide
by Dave Thomas with Chard Fowler and Andy Hunt, or Learning Ruby by Michael Fitzger-
ald (O'Reilly).
Ruby Syntax and Examples
Let's cover the basics of Ruby syntax through the use of real-world examples.
CHECKING SYNTAX
Ruby provides a built-in mechanism to verify that a file contains valid Ruby syntax. You
can check a file's syntax by passing the -c flag and the path to the file to the Ruby inter-
preter.
$ ruby -c /path/to/ruby/file
This will return Syntax OK if the syntax is correct. Otherwise, it will print a stack trace
pointing to the line where the error occurred.
Comments
In Ruby, the hash character ( # ) is used to represent a comment. Comments contain docu-
mentation for your code. Everything after the \# on the same line is treated as a comment in-
tended to be read by humans and ignored by the Ruby interpreter:
variable = 2 # This is a comment
# This is a multiple line comment because each
# line starts with a hash symbol
Search WWH ::




Custom Search