This repository is meant to be a review/exercise in understanding Modules
- Fork, then clone this repository
- Hang out for a minute
- Contemplate the "Before we get started" questions
Take a few minutes to answer these questions. Write your answers down in a notebook, we'll discuss these before starting the exercises!
- What is a
module
? - How is a module different from a
Class
? How are the two similar? - Why do we use
modules
? Which principal of programming does a module help us adhere to?
- Starting in the
warmup
directory, let's take a take a look at thegun_slinger
andsheriff
classes.
- What functionality do these classes share?
- Is this DRY code?
Let's create a module
called Quoteable
to help us DRY this code up.
- In the
Quoteable
module, we'll create a new method for each bit of shared functionality between the two classes. - We'll
Include
the module into both of the classes (don't forget torequire_relative
!) - Then, remove the lines of shared functionality by either deleting or commenting them out
- Call our new methods in the
runner
file to ensure we have the same functionality!
- CD into the
practice
directory
- Take a look at the
saloon.rb
andinn.rb
files. Is there any shared functionality? - Create a module called
Patronable
, andInclude
it in the two classes. - Comment out OR delete the methods currently written in both classes.
- Starting with
inn_spec.rb
, under thedescribe 'utilizing the module' do
block, follow the instructions to complete the exercise. hintpry
might be your new best friend here. Write a method in themodule
, put apry
inside the method, then call said method in your test block. Once inside yourpry
, typeself
to see what you are, and how to access the data you need! - Once you've finished with
inn
, do the same forsaloon_spec.rb
- Why did we call the module
Patronable
? What part of theInn
andSaloon
classes is it responsible for? - If we had another class that had a list of
patrons
, could wemixin
this module?
If you're looking for some additional challenges, take a look at the spicy_challenge
directory! The README
found within will give you a rundown of what you need to do!
At the end of this review, students should be able to answer the following questions:
- How does utilizing
modules
andmixins
allow developers to keep their codeDRY
? - Why do we want to keep our code
DRY
? - What sort of functionality can be
mixed in
to a class via amodule
?