gotcharubyCritical
Difference between a class and a module
Viewed 0 times
moduleandclassbetweendifference
Problem
I came from Java, and now I am working more with Ruby.
One language feature I am not familiar with is the
One language feature I am not familiar with is the
module. I am wondering what exactly is a module and when do you use one, and why use a module over a class?Solution
The first answer is good and gives some structural answers, but another approach is to think about what you're doing. Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app). Classes are about objects; modules are about functions.
For example, authentication and authorization systems are good examples of modules. Authentication systems work across multiple app-level classes (users are authenticated, sessions manage authentication, lots of other classes will act differently based on the auth state), so authentication systems act as shared APIs.
You might also use a module when you have shared methods across multiple apps (again, the library model is good here).
For example, authentication and authorization systems are good examples of modules. Authentication systems work across multiple app-level classes (users are authenticated, sessions manage authentication, lots of other classes will act differently based on the auth state), so authentication systems act as shared APIs.
You might also use a module when you have shared methods across multiple apps (again, the library model is good here).
Context
Stack Overflow Q#151505, score: 436
Revisions (0)
No revisions yet.