Save a Ruby program in a file, then run it with `ruby c2f.rb`. The `ruby` executable is the interpreter: it reads the source file and executes the program. A conversion program that starts with 100 Celsius can print a result of 212 Fahrenheit. section 1.0 section 1.1.6
Check before running
Run `ruby -cw c2f.rb` to check the file without executing it. `-c` checks syntax; `-w` also requests warnings about code that is legal but questionable. If the check reports `Syntax OK`, run `ruby c2f.rb`. A missing closing parenthesis prevents execution, while an incorrect number in the conversion formula can pass the syntax check and produce a wrong answer. section 1.1.6 section 1.4.1
A syntax error may be reported later than the mistake itself: after an opening parenthesis is left unmatched, Ruby can reach the end of the file before deciding that its closing parenthesis is missing. The interpreter checks whether code is syntactically valid; it does not thereby verify the calculation. section 1.1.6
Command-line switches change what the interpreter does:
- `-e` executes quoted Ruby code instead of reading a program file. For example, `ruby -e 'puts "David A. Black".reverse'` prints the name backward. section 1.4.1
- `-l` enables line mode; `-rname` requires a named feature. `-v` shows version information and enables verbose mode, while `--version` shows version information without that additional behavior. section 1.4.1
- `-h` displays information about the available interpreter switches. section 1.4.1
Table 1.6 Commonly used Ruby command-line switches
| Switch |
Description |
Example of usage |
|---|---|---|
|
|
Checks the syntax of a program file without executing the program |
|
|
|
Gives warning messages during program execution |
|
|
|
Executes the code provided in quotation marks on the command line |
|
|
|
Line mode: prints a newline after every line of output |
|
|
|
Requires the named feature |
|
|
|
Shows Ruby version information and executes the program in verbose mode |
|
|
|
Shows Ruby version information |
|
|
|
Shows information about all command-line switches for the interpreter |
|
Compare common switches by purpose and command-line example, including checking a file, running inline code, and inspecting the installed version. section 1.4.1
