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
A namespace can be thought of as a container for other items. These things can be classes, constants, other modules, and more.
Code Samples
Multiple levels of modules can be used to namespace items:
module LaserBots
module Console
class Command
end
end
module World
class Player
attr_reader :name
def initialize(name)
@name = name
end
end
end
end
The classes, modules, and constants can then be accessed by using the constant resolution operator (::) to get to nested namespaces:
player = LaserBots::World::Player.new("Jason")
puts player.name
Now, we're going to talk about namespaces.
0:00
A namespace can be thought of
as a container for other items.
0:03
These things can be classes,
constants, other modules and more.
0:08
The value in namespacing comes from having
potential conflicts of names in our code.
0:13
As an example, let's say that we
were writing a text based game.
0:19
The game has a couple of
different concepts of commands
0:24
that are both completely separate.
0:27
When we start the game
from our command prompt,
0:30
we can add a flag to change certain
options for gameplay or set our name.
0:32
Similarly, our player can
command other characters.
0:37
Let's see how namespaces
work now using workspaces.
0:41
So now, let's take a little
bit of a look at namespacing.
0:45
Right here,
we've got this laser_bots.rb file.
0:49
This is eventually going to be
a game about lasers and robots.
0:54
For right now, we have this class
that represents a player and
1:00
the player just has a name attribute.
1:04
So we initialize the player
to a new player variable, and
1:07
then write the player's
name to the screen.
1:11
Now let's just go ahead and
run that, and watch it work.
1:15
So I'm just going to run
ruby laser_bots.rb, and
1:18
you can see it prints
out the player's name.
1:22
Now if we wanna namespace this, we can do
that by surrounding this with a module.
1:26
So let's go ahead and call this module,
1:34
LaserBots and
we can indent everything here.
1:36
And now, this player class lives
inside of the Laserbots module.
1:46
So let's run this again and
it's gonna fail this time.
1:54
And we can see it says
uninitialized constant player
1:57
and why does it say that,
we clearly have a player class here.
2:03
But the thing is,
it lives inside of the laser bot's module.
2:08
So we need to tell ruby where
the player class is now and
2:12
we do that by typing
the module that it's in first.
2:18
We access it just like a constant,
which we saw in the last video.
2:24
So now we have this player object,
and Ruby knows to look for
2:30
it inside of this LaserBots module.
2:33
So if we were to run this again,
we can see that that works now.
2:37
Now one thing that's interesting is,
we do not have to
2:43
specify the constant resolution
operator if we're inside of a module.
2:47
So let's say we had
a class called robot and
2:54
the robot class was inside
of the LaserBots module,
2:58
we'll say robots have a name also,
3:02
it's pretty much the same as a player
class but it can also target a player.
3:08
In that case, we could call
something like Player.new and
3:20
let's just call this name here.
3:27
In this case, if we are in the same
level of the module hierarchy,
3:31
we don't have to specify that
constant resolution operator.
3:35
And in the same way,
if we really wanted to,
3:42
we could have multiple
levels of name spaces.
3:45
So for example, let's say we had
3:50
a console module inside here and
that dealt with
3:54
writing things to the screen that could
have a command class inside of it.
3:59
And then the same thing if we wanted
to have things that actually dealt
4:05
with the game world,
we could have a module called world and
4:10
that would be where we could put
4:16
the Player and Robot classes.
4:20
And now if we were to run this it would
error out and let's just go ahead and
4:35
do that again and watch it fail.
4:39
Again, we can't find
the constant LaserBots player,
4:41
in that case we need to change
this to LaserBots::World::Player.
4:46
And that will access the class
inside of this module.
4:51
You need to sign up for Treehouse in order to download course files.
Sign up