How to Work with Ruby on macOS / Mac OS X

The macOS / Mac OS X operating system comes pre-installed with the libraries needed to run Ruby programs. You can interact with Ruby programs via the Terminal application (Applications -> Utilities -> Terminal). To determine the version of Ruby installed, type the following in the Terminal window and then hit enter/return:

ruby -v

This will display the version of Ruby pre-installed on the operating system. For example, this may display 1.8.7

Ruby programs can be created using any text editor such as EditRocket. Ruby programs and scripts typically end with the .rb extension. EditRocket will automatically recognize files with the .rb extension as Ruby programs, and will color the syntax accordingly.

To create a Ruby program, simply create a new file, such as hello.rb. In the file, place the following:

puts "Hello, world!"

The above script can be executed using the EditRocket Tools -> Ruby -> Execute Program option, or you can execute it from the Terminal window. To execute the script in the Terminal, use the cd command to cd to the directory where the hello.rb file was saved, such as

cd /Users/user/Desktop

Then type the following:

ruby hello.rb

Hello World! should then be printed to the screen.

For more information about runing Ruby programs via the local Mac Apache web server, see the following:

Running Ruby with the Apache Web Server on Mac