Exercise Wednesday

Goal: The goal for this classwork is to continue practicing Ruby, TDD and building classes. Practice using inheritance, modules and mixins if you have time and these concepts are a good fit for the project you’re working on.

Warm Up


  • How does inheritance work in Ruby? How is it useful?
  • What is the difference between a module and a class?
  • When should we use a mixin instead of class inheritance?

Code


Rock, Paper, Scissors

Recreate the game Rock, Paper, Scissors. This can be a two player game or a game you play against the computer. Write a method that can handle the different combinations of plays and make sure to write specs for all possible combination of plays. After all specs are passing, build a script that a player can run to play the game. Make sure to include a README in your GitHub repo.

You can continue building out functionality for the game, including a tally of wins, ties and losses.

Here’s a first spec to get started:

it("returns true if rock is the object and scissors is the argument") do
  game = RPS.new()
  expect(game.wins?("rock", "scissors")).to(eq(true))
end

Make sure to think about when the game results in a tie, as well.

Note: We won't cover testing randomness so it's okay to skip writing a spec for that part. If you have the time, go for it!

Find and Replace

Create a script where a user can find and replace words in a string. The method in action will look something like "Hello world".find_and_replace("world", "universe"). The result of would be "Hello universe".

Do first: Write the method so that it replaces whole words only, as in the "hello world" example above. Make sure all your tests pass.

Do second: Add to your method so that your method takes into account partial matches. For example "I am walking my cat to the cathedral".find_and_replace("cat", "dog") would return the silly phrase "I am walking my dog to the doghedral."

Extra credit: Refactor your application so that it works with a .txt file. You can use Ruby’s File.open() method to open a file and then perform operations on it.

Make sure to account for odd user inputs like all capitalization, partial capitalization, etc. Include a README for this project and create a Git repository for it.

Palindromes

A palindrome is any word, phrase, number, or other sequence of characters which reads the same backward or forward. Create a method to identify if a word is a palindrome.

The hard way: Create the method without using the reverse() method.

Bonus points: Make your method check a string of words and also an Integer.

Extra credit: Create a script that can scan a .txt file to see if it includes any palindromes. Your script can return the total number of palindromes (a tally) plus the actual palindromes in the script.

Scrabble Score

Refactor your Scrabble project to use inheritance. For instance, you could have a Word class and a Sentence class.

Peer Code Review


  • Do specs have complete coverage for behaviors that need to be tested?
  • Are all specs passing?
  • Is the logic easy to understand? Could it be refactored further, perhaps with inheritance or mixins? Explain.

Lesson 15 of 22
Last updated more than 3 months ago.