Break into pairs to do in class practice coding problems. You should be able to run Ruby on AWS Cloud 9, MathLAN, or on a Mac OS computer. You might need to download Ruby for a Windows PC … or you could also use repl.it and create Ruby repl for this exercise.
To create and run a Ruby program, you should:
- Create a plain text file and save it with the .rb extension.
- From the command line, start the Ruby interpreter and run the program in your .rb file by typing
Problem 1: Iteration over an Array
You have a new client who is interested in creating an invoicing program for an online store. This is very early in development, and all you need to do is get a start at creating a way to save prices on an order and do some simple calculations using two different methods. The prices on an order will vary in number. An order can have anywhere from 1 to n prices, but I don’t expect you to write the code to ask for and accept input … just create an array in your code for testing for now.
Method 1: add all of the prices together to get a total
Method 2: take 1/3 off each price on an order and calculate (and display) the discount
For now, let’s say we are in testing mode. Rather than worry about accepting user input, create in your code an arbitrary collection of prices that will be used for testing the methods.
Have your program:
- Print to the screen the list of prices you are using to test.
- Print the total of the prices
- Print the discount
Problem 2: Guess Random Number
Write a program to play ‘guess the number’. Your program should:
- Generate a random number between 1 and 100 using ‘rand(100)’
- Ask the user for a guess using ‘gets’
- Use a conditional to check if the user guessed correctly or needs to try again
- Limit the user to 10 guesses
Problem 3: Classes and Inheritance
You are very busy and have another client who wants to develop an app for sharing videos, music, photos, etc. After you and your team stop rejoicing, you get down to work in developing the classes and class methods you will need for this project. For now, just focus on defining the classes for videos and music and start with very limited functionality, but keep in mind that you will almost certainly be adding features in subsequent sprints.
You notice that music and videos have some of the same functionality: the client thinks that users will 1) want to play songs and videos and 2) leave comments on them.
However, music and videos also have significant differences. We often classify songs by beats per minute whereas we care about frames per second, display size, and resolution on videos. So there are similarities and differences. So, you want to create a superclass that encompasses the similarities and have movie and video subclasses inherit those but extend the functionality for attributes that are different.
Again, you don’t have to create the user interface. Use embedded, “dummy” data when creating objects for testing and output.
Have your program:
- Define a superclass with attribute reader for comments, an initialize method, an add_comment method that uses ‘push’, and a play method (which just prints “Playing” or something)
- Define two subclasses that inherit from the superclass and extend functionality by adding a resolution attribute for videos and a beats per minute attribute for music using attr_accessor
- Create a video
- Create a song
- Add comments for both the song and the video (these can be hard coded testing method calls rather than reading input)
- Print out the comments for the song and the video
Note: both of these are from the book Head First Ruby by Jay McGavren. The book is highly readable, but it does walk you through concepts slowly and in detail. I personally find it very useful, but its pace may be frustrating for others. You might want to use these exercises from the Head First Ruby site to practice more Ruby.
For More Practice
Try working with blocks: Head First Ruby Chapter 5 exercises
- For problem 2, allow the user to choose what range and number of guesses they want to play with
- For problem 2, print out a “congrats” or “sorry you lose message” depending on the outcome
- For problem 3, have the play method use reflection to state “Video is playing” or “Song is playing”
- For problem 3, allow the user to add a comment through the terminal using ‘gets’