Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we now how modules work, we’re going to write a simple program that simulates keeping inventory at a small retail store. The store sells shirts, pants, and accessories. We’re going to put everything together using modules, include, extend, and friends. Make sure to follow along using Workspaces.
Code Samples
First, we'll create our module that will hold our inventory methods
module Inventoryable
end
class Shirt
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
class Pant
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
class Accessory
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
shirt1 = Shirt.new(name: "MTF", size: "L")
shirt2 = Shirt.new(name: "MTF", size: "M")
[MUSIC].
0:00
Now that we know how modules work,
we're going to write a simple program that
0:04
simulates keeping inventory
at a small retail store.
0:08
The store sells shirts,
pants and accessories.
0:13
We're going to put everything together
using modules, include extend and
0:16
their friends.
0:21
Make sure to follow along
now using Work Spaces.
0:23
Okay so let's go ahead and
create our store.
0:27
So first what we're going to
do is create a new file, and
0:31
we're just going to call this Store.rb.
0:35
Now our store is going to sell shirts,
pants and accessories.
0:41
So let's go ahead and
create a class for our shirts.
0:47
And the shirt is going to
have an attribute accessor.
0:53
Called attributes.
0:59
And that's going to hold stuff like
the name of the shirt, the size and
1:01
maybe whether or not it's in stock.
1:06
So when we instantiate
an instance of the shirt class,
1:14
we're going to internally
set the attributes,
1:20
[SOUND] to the attributes
that are passed in.
1:25
So now let's go ahead and
initialize a shirt,
1:30
we'll say shirt.new,
name MTF for Mike the Frog.
1:35
And this comes in a size large.
1:41
And we can also say shirt 2, we'll
change that to shirt 1, shirt.new name
1:47
Mike the Frog, and
that comes in size medium.
1:53
Okay.
2:01
Well that looks fine.
2:02
I mean, this is something that
we've already seen before.
2:03
Let's go ahead and
make sure that we have pants too.
2:07
And actually,
pants are going to do the same thing.
2:13
So we can copy that.
2:18
And paste it there.
2:22
So now what we want to do is create
the pattern that we saw before
2:24
over in our tracking class here,
where we had this tracking module,
2:29
we can create, access the instances,
and find them as well.
2:35
So we want to do the same thing for
our store.
2:42
Now we're gonna initialize pants and
2:45
also create the accessories
class a little bit later.
2:47
Accessory we'll do now.
2:51
[SOUND] Okay, so
2:52
we have our three different classes,
shirts, pants and accessories.
2:57
But what we wanna do is be able
to track all these in inventory.
3:03
So, we're gonna make a module for that.
3:09
And the module is called Inventoryable.
3:12
Which just says that whatever class
3:17
this module is included in is going
to be able to have something.
3:20
In inventory.
3:23
Now we'll get to that in just a moment.
3:26
In our next video we're going to start
including different class methods and
3:28
writing those in our inventoryable module.
3:34
You need to sign up for Treehouse in order to download course files.
Sign up