Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Cloud Services
AWS
Azure
Google Cloud
Salesforce
Microsoft
SAP
February 18, 2025
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.