Worksheet Week 12: Introduction to Ruby

We will be using the Hackety Hack environment (hacketyhack.net) for this, which is an easy to install Ruby environment for Windows. If you run Mac OS X or Linux at home, you can develop Ruby programs by creating them in a text file, saving them with the extension .rb and running them with:

ruby myprog.rb

  1. Write a program which stores the year by year HitTastic! downloads in an array. Do not store the year in the array, just the number of downloads. Read in a year from the user, subtract 2003 from it to get an array index, and print the number of downloads for that year on the screen. To read in something from the user, use gets, e.g.
    year=gets
    Year No. of downloads
    2003 50000
    2004 100000
    2005 200000
    2006 500000
    2007 900000
    2008 1900000
  2. Create a Hit class. This should have:
  3. Create five actual Hit objects representing the following songs:
    Chemical Brothers, Galvanize, chart 3, year 2005
    Snow Patrol, Run, chart 4, year 2004
    Electronic, Getting Away With It, chart 12, year 1989
    Genesis, Turn It On Again, chart 8, year 1980
    The X-Factor Winner, I Am The Greatest Ever, chart 1, year 2020
    
  4. Write a program which stores the following information in a hash:
    Name Room number
    Bob Backhouse RM801
    Mark Cranshaw RM802
    Sandra Revell RM803
    John Flackett RM804
    Read in a person's name from the user, and display the corresponding room number. To read in text from the keyboard, use:
    name=gets
    The variable 'name' will then contain the text read in.

Advanced