Reputation: 11
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?
Upvotes: 1
Views: 484
Reputation: 36101
Generally speaking modules have two uses:
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).
Upvotes: 1