Let’s take an example to better understand the differences.
When you use include inside a class, it adds the methods from the module Foo to that class. This means that only instances of that class will have access to the module’s methods.
Example:
Result: greet is available to instances of MyClass
When you use include outside any class, it adds the module Foo to Object’s ancestors. Since all objects inherit from Object, the methods in Foo become available for all objects.
Example:
Result: greet is now available for all objects (like String, Array, etc.)
Inside the class: Use this when you want the module’s methods to be available only in specific classes.
Outside the class: Use this when you want the module’s methods to be available globally across all objects.
While using include inside a class is more common and safer, using it outside can be helpful for adding shared behavior across all objects.