CSE 341 Lecture Notes - Running Ruby - Quickstart

Ruby itself is designed to load and run files, but doesn't have an interface for interactive use. For interactive use, use 'irb'. You can start up interactive Ruby, and type expressions to be evaluated. Use the load function to load in a program file. Type control-d to exit.

Unfortunately there are a plethora of versions. The latest stable release is 2.2.3, which is what is installed on the undergrad linux and windows machines. We are hoping that any version 2 release will be OK for 341, but if in doubt the Official Version for this quarter is 2.2.3, since that is what is on attu.

Type irb at the command prompt to get Interactive Ruby. I suggest using the --prompt simple and --noinspect options to get Ruby to have a less verbose prompt, and to use to_s to print the results of evaluating an expression in the top-level read-eval-print loop. In other words use this:

irb --prompt simple --noinspect

If you don't want to type this every time, put the following in a file named .irbrc in your home directory:

IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:INSPECT_MODE] = false

You will need to be able to run the tk graphics package and the unit test package. You can test whether these are on your machine by starting interactive Ruby using irb. Then at the prompt type

  require 'tk'
  require 'minitest/autorun'
  

CSE Lab Machines - Windows

Open a command shell, either by running cmd (a Windows shell) or a cygwin bash shell. Use the cd command to switch to the directory where your Ruby file is (using the Windows shell you separate directories with backward slashes (\) and with cygwin you use forward slashes (/)). After you are in the right directory, type irb to start the Ruby REPL. If you are running a batch program (for example running unit tests), use

ruby mytests.rb

CSE Lab Machines - Linux

Type irb at the prompt to get Interactive Ruby (or better irb --prompt simple --noinspect).

To run Ruby on a program file type

ruby yourfilename.rb

The unit test framework may not work out of the box with the linux version of Ruby. If so, to make it work, type this at the command prompt:

gem install minitest
You only need to do this once. (In case you're curious, the gem will end up in this subdirectory .gem/ruby/gems of your home directory.)

Personal Machines

You may already have Ruby on your machine -- make sure you have a version 2 of some sort however. The TAs will be testing your programs using 2.2.3, so if you're using another version, please test your program on 2.2.3 (for example on attu) before turning it in.

You can download Ruby for various platforms from the Ruby home page. It's free.

The Macintosh OS comes with Ruby. On Yosemite, the installed version includes tk, which works fine. On El Capitan, it doesn't -- see the directions below for how to fix it. Hopefully these will work for you, but in the worst case you will need to do Assignment 7 on a CSE undergrad lab machine. (Assignment 8 doesn't use tk or graphics, so you will be able to do it on a personal Mac in any case.) One other issue: if you use the 2.0.0 installed version of Ruby, change the class declaration for TestPosRational to make it a subclass of Minitest::Unit::TestCase (see the comment), which is the older version. You can turn in tests using this version -- Ruby will give a warning about something obsolete but it will run in 2.2.3.

Installing Ruby on El Capitan: