Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Write a Simple Module3:35 with Jason Seifer
Writing a module is very similar to writing a class. Modules follow the same rules as classes and use CamelCase for naming. In this video, we're going to write the simplest possible module in Ruby.
Here is our simple module:
module SimpleModule end
We can load it in to
irb by using the
load keyword and passing the file path:
irb> load "./simple_module.rb" => true
We can add a constant inside of the
SimpleModule by typing it's name in all upper case letters:
module SimpleModule VERSION = 1.0 end
That can then be accessed using the constant resolution operator (::):
irb> SimpleModule::VERSION => 1.0
Now let's go ahead and write our first module.
Writing a module is very similar to writing a class.
Modules follow the same rules as classes and use camel case for naming.
When we create a module, we use the Ruby keyword Module,
followed by the name of the module in CamelCase and then the keyword end.
Let's see how that works now using WorkSpaces.
Okay, so let's go ahead and see how to write a Ruby Module.
Now I've gone ahead and launched a new Ruby workspace.
So go ahead and close this welcome markdown file.
Now over on the left side right click and choose new file.
We're going to name this simple_module.rb And
now, it will open up in the main text editor.
Writing a module is just like writing a class except instead of
using the class keyword, we use module, so
type in the module keyword and then the name of the module that you want.
Now I'm going to call this SimpleModule and
then press enter and write the word end.
Now let's go ahead and see how this works inside of irb.
Click down into the console on the bottom of the screen and type in irb.
Now we have to get our simple module program loaded into irb.
We do that by typing load, then a space, quote and
then the path to the simple module file.
Which is right here so type ./ and then the name of the file,
which is simple_module.rb and irb will return true.
So now you might wonder how we access simple module.
Now we can't initialize it like a class.
So normally we could say class.new, but
we can't do that when we're working with modules.
Just accessing it will return SimpleModule.
So this doesn't do much right now, but
what we could do if we wanted to was store a constant in here.
So lets say we wanted to return the version of this SimpleModule.
We could create a constant called VERSION and set that equal to 1.0.
Now constants are their own thing, in Ruby kind of like modules,
they're written in all upper case, and they can't be changed.
So once we type in this version here, it's gonna be locked at 1.0.
Now since we've changed this file and
saved it, we need to reload it into our irb session.
So I'm going to clear the screen and in order to do that,
I'm going to press the up arrow a couple of times
until that load statement comes back up and there we go.
Now if we want to access the version inside of this module,
we do that by typing SimpleModule, and
then two colons, and the word VERSION and that will return 1.0.
When we access something using with two colons, it's called the constant
resolution operator, and that's what we use to access something inside of modules.
Try creating a module on your own now using WorkSpaces.
You need to sign up for Treehouse in order to download course files.Sign up