CS 3723
  Programming Languages  
  Ruby Access   


Accessing Ruby at elk01:
  • Simplest way: In the Linux lab, Ruby is only available through secure shell to the elk01 machine. You can just type ruby. Again in the Linux lab, the full path to ruby is /usr/bin/ruby. If you have Ruby on your system, it may be somewhere else, say, at /usr/local/bin/ruby. You can find the path to ruby on a system by executing:
      % which ruby
      /usr/bin/ruby
      % ruby -version
      ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
      

    The command ruby -version above gives the version of Ruby running on your system.

  • Command-line Ruby: To run a Ruby file, first create it with an editor, or download it, etc., say as pi.rb (here and below in web pages we will often give the source of a Ruby file as an .html file), which is a program that calculates 15 digits of pi. Your file can have any extension, or none at all, but we will often use .rb. Then at the Unix % prompt you just type ruby pi.rb, and it runs.

  • Command-line Ruby more directly: You can also put in a special line as the first in your program containing the following (assuming of course that Ruby on your system is at /usr/bin/ruby).
      #!/usr/bin/ruby
      

    Then you have to make the file pi.rb executable, using the command % chmod +x pi.rb, and you can invoke Ruby to run the program by just typing its name: pi.rb, at the Unix % prompt. So now download pi.rb, make it executable, and execute it, by typing ./pi.rb when the file is in your current directory. (This is the way the elk01 system and my Linux system at home are set up: You have to type ./xyz to execute a file xyz in the current directory.)

  • An amazing Ruby program: The program pi2.rb, if executed as above, will print arbitrariy many digits of pi. The digits just spew out at your terminal. Be careful if you run this: you must be ready to interrupt it immediately with Ctrl-C.


Downloading Ruby to Your Home Machine:
  • Ruby runs on almost any version of Unix/Linux, Mac, and even on Xxxindows. On all systems it is a free download. See the Ruby Download Page.

Online Ruby Through a Browser:
  • In case you don't have Ruby at home and can't access an elk machine, you can run Ruby from any browser through the tutorialspoint site, on the page: compile online. (Rats! This isn't working for me now.)

Ruby in debug mode: Ruby has a fancy debug mode available:

    Ruby can be run in debug mode using ruby -r debug [additional options]
    Options for debug mode are give at: Debug Options.

      % ruby -r debug pi2.rb
      Debug.rb
      Emacs support available.
      
      pi.rb:2:k, a, b, a1, b1 = 2, 4, 1, 12, 4
      (rdb:1) list 1-13
      [1, 13] in pi.rb
         1
      => 2  k, a, b, a1, b1 = 2, 4, 1, 12, 4
         3  loop do
         4    p, q, k = k*k, 2*k+1, k+1
         5    a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
         6    d, d1 = a/b, a1/b1
         7    while d == d1
         8      print d
         9      $stdout.flush
         10      a, a1 = 10*(a%b), 10*(a1%b1)
         11      d, d1 = a/b, a1/b1
         12    end
         13  end
      (rdb:1) b 4
      Set breakpoint 1 at pi.rb:4
      (rdb:1) c
      Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) c
      3Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) c
      1Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) c
      Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) c
      4Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) c
      1Breakpoint 1, toplevel at pi.rb:4
      pi.rb:4:  p, q, k = k*k, 2*k+1, k+1
      (rdb:1) exit
      %
      

    Notice that as this program runs, after repeated c or cont commands (for "continue execution"), the program loops over and over to the breakpoint that was set at line 4. As it executes, it is outputting the initial digits of pi, shown in red above.

    Here are 14 of the 33 commands given at the link above. You need not use the complete command name, but the part given inside [...] is optional.

    SNCommand with Description
    1b[reak] [< file| class>:]< line| method>
    Sets breakpoint to some position.
    4b[reak]
    Displays breakpoints and watchpoints
    5del[ete] [n]
    Deletes breakpoints
    6disp[lay] expression
    Displays value of expression
    7undisp[lay] [ n]
    Removes display of n
    8c[ont]
    Continues execution
    9s[tep] [ n]
    Executes next n lines stepping into methods
      
    SNCommand with Description
    10n[ext] [ n]
    Executes next n lines stepping over methods
    13l[ist][<-| n- m>]
    Displays source lines from n to m
    17tr[ace] [on|off]
    Toggles trace mode on and off
    18q[uit]
    Exits debugger
    19v[ar] g[lobal]
    Displays global variables
    20v[ar] l[ocal]
    Displays local variables
    32h[elp]
    Displays help message


Revision date: 2013-11-05. (Please use ISO 8601, the International Standard Date and Time Notation.)