I think both are used for namespace. Modules are generally mixed into classes, right? So, what would be the purpose of defining a module inside a class?
Asked
Active
Viewed 469 times
1 Answers
1
Generally speaking modules have two uses:
- Namespacing modules. When you nest stuff here and the module is intended only for specifying paths.
- Functional modules. When the module has actual functionality that is intended to be called directly on the module or the module is intended to be included/extended.
Classes should be used for functionality only, even though Ruby doesn't enforce it.
Trying to do something but the above (like use module for both namespacing and functionality (1) or use a class for namespacing (2)) will generally confuse you.
(1) Some will disagree pointing to the rails' module with instance methods that also holds another module, called ClassMethods
. I think it would have been cleaner if there was a module with two modules - ClassMethods
and InstanceMethods
instead.
(2) Some will disagree. A probably valid case is if you try to emulate private classes from other languages (where the private class will be nested inside your public class).

ndnenkov
- 35,425
- 9
- 72
- 104