ICS 492 Ruby on Rails

Metropolitan State University

Spring 2008

 

Practice Week #1

 

I.  Getting Started with IRB

 

1. Open a DOS Window

 

            Start -> Run -> Command

 

2.  Change directories to the Instant Rails directory where Ruby is installed

 

            cd InstantRails2 (or cd instan~1)

 

If for some reason this does not work, find the correct short version of this directory name using

 

            dir /x

 

then use the short name for the directory when you use the CD command.

 

3.  Run InstantRails to set up the environment for Ruby and Rails. 

 

            InstantRails

 

You will receive a series of warnings about the web server and database server.  We won’t be using either today.  Ignore the warning messages by clicking Yes or OK.

 

You’re not an administrator.  Some actions will not be available.  Do you want to continue?

Apache port (80) is used by “” (inetinfo.exe)

MySQL port (3306) is used by “” (mysqld-nt.exe)

 

An InstantRails window should pop up.

 

4.  To start today, we’ll be using Interactive Ruby (IRB).

 

If the InstantRails window is not showing, double click the red “I” on your bottom menu bar.

Click the black “I” in the upper left corner of the InstantRails window.

Choose Rails Applications -> Open Ruby Console Window

Type IRB at the DOS prompt C:\InstantRails2\rails_apps>

 

 

II.  Practice with Ruby and IRB

 

Recommendation:  Take notes on the lines of Ruby code you successfully write.

 

1.       Create an array of colors.  Iterate over the array, displaying the contents of the array

 

2.       Capitalize the first letter of each color.  Do not change the contents of the original array.

 

3.       Capitalize the first letter of each color without changing the contents of the original array and concatenate all of the colors into one string.

 

4.       Capitalize the first letter of each color, changing the contents of the original array.

 

5.       Sort the array in reverse alphabetical order.

 

6.       Create a string which contains punctuation, spaces, newlines and words.  Eliminate all the punctuation, spaces and newlines and redisplay the string.  (Hint:  Use split and join)

 

7.       Open a file and echo its contents to the screen.  You can hardcode the filename.

 

8.       Create a hash table with keys ‘red’, ‘green’, ‘blue’ and ‘other’.  Iterate over your array of colors, counting the number of times each color appears in the array.  Display the contents of the hash table.

 

9.       Make the previous step into a method named ‘count’ with one argument, an array of colors.  Return and display the hash table.

 

10.   To end

 

Type EXIT to end IRB

Leave the DOS windows open for now.  You’ll be using them again later.

 

 

III.  Running Ruby from TextPad

 

Now that you’ve spent a bit of time using Ruby in interactive mode you are probably wondering how to put together and save a whole program.  Go ahead and open TextPad now.

 

Start -> All Programs -> TextPad

 

1.  Using TextPad, type in the same Ruby commands you created in the previous section of this lab.  Save the program as yourfilename.rb

 

2.  “Run” the Ruby program

 

Tools-> Ruby

 

TextPad submits this set of commands to the Ruby interpreter and displays the results.  Unlike IRB, the TextPad results window only displays the data which the program explicitly outputs.

 

 

IV.  Running Ruby Programs from the DOS Prompt

 

At the DOS prompt, type

 

ruby yourfilename.rb

 

If you wish to provide a command line argument, type

 

ruby yourfilename.rb additionalInfoOfYourChoice

 

1.       Run the program you created in Step III from the DOS prompt.

 

2.       Alter your program to accept a file name from the command line (Part II.7).  Run your program from the DOS prompt.

 

3.       To end

 

Type EXIT to close the DOS window in which you were running your Ruby programs.

If the InstantRails window is not showing, double click the red “I” to on the bottom menu bar.

Click the black “I” and Exit

Close the remaining DOS Window by typing EXIT

 

V.  Installing Ruby on Rails at Home

 

 

Do Not Do This in the Metro State Labs

 

 

Download InstantRails from http://instantrails.rubyforge.org/wiki/wiki.pl  and follow the “installation” instructions.  “Instant Rails is a one-stop Rails runtime solution containing Ruby, Rails, a web server and a DBMS, all pre-configured and ready to run. No installer, you simply drop it into the directory of your choice and run it.”

 

. 

VI.  Configuring TextPad

 

If you wish to use TextPad at home as we did tonight in lab, use this setting:

 

Configure -> Preferences -> Tools -> Add -> Program

File name: C:\InstantRails2\ruby\bin\ruby

 

This is the path name of the ruby executable.  The path name may be different on your home computer, depending on where you choose to install InstantRails.

 

You are not required to use TextPad but you will need a plain text editor of some sort.