CSCI 485 Lab 1: ruby and basic generative programming

Printer testing: if you want to test your print script without sending something to an actual printer, you can now also use the printer name nullp. (Thanks jim!)

The objectives for this lab are to get an introduction to the Ruby programming language and some practice in writing programs that generate other programs.
(Assignment 1 involves creating a ruby program whose purpose is to create or replace a bash script for the user.)

Two lab sessions (Jan 19th and 26th) have been allocated for working through lab 1 and beginning your work on assignment 1, which is due on January 30th.

Lab 1 part A: intro to ruby

While we'll be expanding on our ruby knowledge in later weeks, here we'll consider just the core components necessary for the first assignment:

In the lab itself we'll just go over the basic ruby sytnax to handle the task at hand, I'd strongly recommend practicing with more extensive examples as time permits. There are lots of good ruby references online, my own quick reference is here, but I'm sure you can find better.

Below are some (hopefully) useful snippets of ruby that you might find applicable during the assignment:

# take a line of text and split it into words, putting the words into an array
wordsArray = line.split  # splits words by whitespace, first word in wordsArray[0]

# reading a line of input and getting rid of the newline at the end
userInput = gets.chomp

# converting text to an integer (returns 0 if the text doesn't represent an int)
number = text.to_i

# forcing the first character of a string into uppercase
str = str.capitalize

# seeing if the start of a string matches another string
if text.start_with?("XYZ") then
   .......
end

# creating an array of size N
array = Array.new(10)

# concatenating strings
str = "hello" + " "
str += " world"


Lab 1 part B: programs that write programs

In assignment 1 you will be writing a ruby program that creates a customized bash script - the same basic concept as much of generative programming, just on a small scale.

For simple cases, this amounts to:

  1. identifying what sequence of actions the generated script is supposed to perform
  2. create the text strings corresponding to each of the desired actions
  3. write the content to the file
  4. make the generated file executable

Often there will be a skeleton or template you can use for the bulk of the file being generated, with spots to include any extra generated text.

That is certainly the case in assignment 1, where you are building a print (lpr) command, and inserting that in the midst of an otherwise static script.

The generated script might look very much like this:
#! /bin/bash
for file in "$@" ; do
    if [ -f $file ] ; then
       ### THE CREATED LPR COMMAND WOULD GO HERE ###
    else
       echo "file $file not found, not printed"
    fi
done

Thus your solution for the assignment might look something like

  1. ask the user questions to find out what print options they want
  2. build a string containing the appropriate lpr command
  3. open myprint.sh for writing
  4. print the first three lines of myprint.sh
    #! /bin/bash
    for file in "$@" ; do
        if [ -f $file ] ; then
    
  5. print the lpr string you built
  6. print the last four lines of myprint.sh
        else
           echo "file $file not found, not printed"
        fi
    done
    
  7. close the file

Remember to make the file executable by running the shell command
chmod u+x myprint.sh

Error checking will be an important part of getting the construction options: