liveBook · Manning

Ruby interpreter

As explained in The Well-Grounded Rubyist, Fourth Edition

The Ruby interpreter is the executable program that reads Ruby source programs and executes them.

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

-c

Checks the syntax of a program file without executing the program

ruby -c c2f.rb

-w

Gives warning messages during program execution

ruby -w c2f.rb

-e

Executes the code provided in quotation marks on the command line

ruby -e 'puts "Code demo!"'

-l

Line mode: prints a newline after every line of output

ruby -le 'print "+ newline!"'

-rname

Requires the named feature

ruby –rprofile

-v

Shows Ruby version information and executes the program in verbose mode

ruby –v

--version

Shows Ruby version information

ruby –-version

-h

Shows information about all command-line switches for the interpreter

ruby –h

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

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through file execution, syntax checks, warnings, and common switches, without describing interpreter internals or cataloging every switch.
Examples
Uses a Celsius-to-Fahrenheit program, an unmatched parenthesis, and a one-command name reversal.
What it assumes
Assumes some programming experience while introducing how to feed program files to `ruby` and use its switches.
What you can do after
Operate/configure it to run a Ruby file, check syntax without execution, request warnings, and execute short inline code.
How it connects
Links source files and syntax errors to warnings, command-line switches, and Ruby version information.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.