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.
attrmethod and why are these methods useful?
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.
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
primeequal 2, the first prime number.
prime, remove all multiples of
primefrom the list.
number, all the remaining numbers in the list are primes.
You also might find this video helpful in explaining the Sieve.
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
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
spec directories. (Check out Ruby’s
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:
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
triangle_spec.rb might include:
require 'triangle' describe('Triangle') do end
Lesson 20 of 22
Last updated August 7, 2022