Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Ruby Module Basics Namespaces

Andrew Walters
Andrew Walters
8,876 Points

Namespacing in Ruby

I seem to be confused about the use of Namespacing in Ruby. I was under the impression that it was best practice to keep classes seperate and in their own rb file, so why then do we use modules to namespace multiple classes in the same file? Is this just circumstantial such as the video game example or is this a commonly used style? Thanks

Taylor Boudreau
Taylor Boudreau
7,285 Points

Hey Andrew,

I had similar questions and found some great information here:

https://rubymonk.com/learning/books/1-ruby-primer/chapters/35-modules/lessons/80-modules-as-namespaces

As the above link mentions, namespacing allows you to group similar objects, but also load multiple libraries without clashing. I would imagine it's not generally ideal to have global classes, but with limited experience, hopefully someone else can chime in on the practical use of namespacing.

hey man, also a noob here. haha. I believe it's because it's a way to group and create a scope for similar things. so it could be methods, classes, other modules. The question is whether or not inner scopes can access all of its outer scopes, as well as the parent of their parent scopes.

1 Answer

Todd MacIntyre
Todd MacIntyre
12,248 Points

Here is an interesting article I found relating to your question. It also talks a bit about the history behind '::' being used for calling class methods.

https://www.practicingruby.com/articles/uses-for-modules-1