Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
Code Samples
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up