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 trialAndrew Walters
8,876 PointsNamespacing 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
Derek Lin
1,563 Pointshey 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
12,248 PointsHere 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.
Taylor Boudreau
7,285 PointsTaylor Boudreau
7,285 PointsHey 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.