You can think of the module and the methods and constants inside it as providing functions and utility functions that you can include in other objects as you wish. For example, if you want to use the destroy function in Foo and Bar objects, you would do the same:
class Foo include Destroy
Now any Foo or Bar object has access to all methods or constants inside the destruction.
Modules define a namespace, a sandbox in which your methods and constants can play, without worrying about other methods and constants attacking them. ruby docs goes deeper about this and includes a good example when you want to use it, as shown below:
module Debug def whoAmI? "#{self.type.name} (\##{self.id}): #{self.to_s}" end end class Phonograph include Debug # ... end class EightTrack include Debug # ... end ph = Phonograph.new("West End Blues") et = EightTrack.new("Surrealistic Pillow") ph.whoAmI? » "Phonograph (#537766170): West End Blues" et.whoAmI? » "EightTrack (#537765860): Surrealistic Pillow"
In this example, every class that includes Debug has access to the whoAmI? and other methods and constants that Debug includes, without having to override it for each class.
source share