I have read in many textbooks that
In Ruby,a class can only be a subclass of one class. Mixins, however, allow classes without a common ancestor to share methods.
In practice, whenever I need to implement multiple inheritance. I have use Modules & not mixins. for example:
Module name_goes_here
def method_name_goes_here
.....
end
end
Then, I just include them in a class
class MySubClass < MySuperClass
include module_name
end
now, I have referred to multiple ruby books each talking about mixins & then suddenly, all of they start talking about modules without making it clear what is the relation of mixins & modules.
so, Question is:
Are modules == mixins in ruby? if yes, then why. if no, then what’s the difference?
PS: sorry, if its a silly question
Mixins are a language concept that allows to inject some code into a class.
This is implemented in Ruby by the keyword
includethat takes aModuleas a parameter.So yes, in Ruby, mixins are implemented with modules. But modules have other uses than mixins.
For example, modules can also be used for namespacing your classes or encapsulating utility functions to keep from polluting the global namespace.