- Break down problems into implementable pseudocode
- Implement a basic Ruby class and identify when to use instance variables
- Use if/else statements, string methods, while/until loops, Enumerable#each methods
- Find and use built-in Ruby methods to solve challenges
- Explain how instance variables and methods represent the characteristics and actions of an object
Before starting this challenge, you will want to read a bit about classes and ruby objects. Try these: Writing our own Class in Ruby, Ruby Classes and Objects, or read about classes in The Well-Grounded Rubyist (Book). You definitely want to research this before you try it. Classes are a different thing altogether and may be difficult to understand at first. Don't worry, you'll get lots of practice this week! You may also want to get a head-start on Object-Oriented Design by reading Practical Object Oriented Design in Ruby (Book)(affectionately known as POODR).
Implement a basic Die
class which can be initialized with some number of sides. We can then roll the die, returning a random number. It should work like this:
die = Die.new(6)
die.sides # returns 6
die.roll # returns a random number between 1 and 6
If we pass Die.new
a number less than 1
, we should raise an ArgumentError
. This is done using the raise
keyword. See the ArgumentError documentation for how to do this.
Use the Ruby Docs to see how to return a random number.
Translate at least 3 of the tests into Driver Test Code and include it in the driver code section. If the tests are failing to catch a problem, try writing your own driver test code for it.