Exercise Thursday

Goal: With this classwork, your goal is to continue practicing and understanding all of the skills you've been learning in this course section, especially TDD, unit testing and creating custom classes. You are also encouraged to explore Enumerable methods, `attr` methods where needed and inheritance and modules.

Warm Up

• What is an `attr` method and why are these methods useful?
• What is the Enumerable module and what are some of its most useful methods? (Take some time to look over the Enumerable documentation if you haven’t already.)
• What is the difference between `map()` and an `each()` loop?

Code

Coin Combinations

Create a script that lets a user enter an amount of cents and shows the smallest number of quarters, dimes, nickels, and pennies needed to make that change. You may want to use an `until` loop for this project. Make sure to use TDD and unit test your code extensively.

Prime Sifting

Given a number, write a method that returns all of the prime numbers less than that number.

This is a tricky problem and you should use the Sieve of Eratosthenes to solve it. Here's how the Sieve of Eratosthenes works to find a number up to a given `number`:

• Create a list of numbers from 2 through n: 2, 3, 4, ..., `number`.
• Initially, let `prime` equal 2, the first prime number.
• Starting from `prime`, remove all multiples of `prime` from the list.
• Increment `prime` by 1.
• When you reach `number`, all the remaining numbers in the list are primes.

You also might find this video helpful in explaining the Sieve.

Allergies

Here's something a bit trickier. An allergy score is a single number that tells what someone is allergic to. The scores for each allergen are:

``````allergen     score
eggs         1
peanuts      2
shellfish    4
strawberries 8
tomatoes     16
chocolate    32
pollen       64
cats         128
``````

If someone is allergic to eggs and strawberries, they get a score of 9.

Build a script that uses a method that is called on someone's score — i.e. the score is the object, and returns an array listing what they're allergic to. For example, if the value `3` were passed into the method, it would return `["eggs", "peanuts"]`.

Ruby Project Creator

If you’re interested in scripting, here’s a fun challenge. Make sure you complete Coin Combinations before taking on this project.

Many frameworks such as Rails allow you to create the entire structure for a project with a single line of code in the terminal. Your goal is to create a script that will do the same for a basic Ruby project.

First, your script should automatically make all the directories you need for a Ruby project. This includes the root directory and the `lib` and `spec` directories. (Check out Ruby’s `FileUtils` library.)

Next, your script should create a `Gemfile` and put it in the root directory. (Check out the `File.open()` method if you haven’t already.)

Then your script should provide a prompt to users that allows them to enter the name of any classes they plan to include in their application. For instance, a project called `shapes` might have the following classes: `Triangle`, `Square`, `Circle`. Your script should then create `.rb` files and `spec.rb` files for each of the classes in the appropriate directories.

Finally, your script should add a little basic code to these created files. For instance, `triangle.rb` might include:

``````class Triangle
End
``````

Meanwhile, `triangle_spec.rb` might include:

``````require 'triangle'

describe('Triangle') do
end
``````

Happy scripting!

Peer Code Review

• Do specs have complete coverage for the behaviors that need to be tested?
• Are all specs are passing?
• Is logic easy to understand throughout?
• Does code have proper indentation and spacing?
• Are variable names descriptive?
• Does the project have good Git documentation (regular commits with clear, consistently formatted messages)?

Lesson 20 of 22
Last updated August 7, 2022