Skip to main content

Ruby Programs

A Ruby program is a special type of plaintext file, the type of file written in a program such as Microsoft Notepad. Conventionally, the files are named with a ".rb" extension, and for simple scripts as are discussed in this manual, the Ruby file is run from a command-line interface by invoking the Ruby interpreter program with the filename.

$ ruby filename.rb

If you'd like to follow along with the examples and test any algorithmic scripts, you'll first need to download and install the Ruby interpreter. This can be done at https://ruby-lang.org.

Ruby can also be installed using a system package manager such as Homebrew, apt, or Chocolatey.

> choco **install** ruby

A Simple Program

Test the installation of Ruby by creating a plaintext file with the following valid Ruby code. In this example the file is named "simple.rb."

SIMPLE.RB
puts "this is a simple ruby script"

Once it is saved, invoke the Ruby interpreter with the file name and see the output.

COMMAND LINE
> ruby simple.rb
OUTPUT
this is a simple ruby script

It is not strictly necessary to follow the demonstrations of Ruby syntax with this text editor and invocation pattern. Although, if you are keen at learning, particularly for career goals, it is recommended. The material recommends initially "coding" the algorithmic exercises using a pencil and paper, yet they should be verified using the script runner.