{"id":12043,"date":"2025-02-18T05:18:45","date_gmt":"2025-02-18T05:18:45","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12043"},"modified":"2025-02-18T05:18:45","modified_gmt":"2025-02-18T05:18:45","slug":"inside-vs-outside-module-inclusion-in-ruby-on-rails","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/inside-vs-outside-module-inclusion-in-ruby-on-rails","title":{"rendered":"How to Include Module Works Inside vs Outside the Class"},"content":{"rendered":"<p>Let\u2019s take an example to better understand the differences.<\/p>\n<h3>include Foo Inside a Class<\/h3>\n<p>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&#8217;s methods.<\/p>\n<p><strong>Example:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051524\/unnamed-3.png\" alt=\"output\" width=\"480\" height=\"292\" class=\"alignright size-full wp-image-12045\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051524\/unnamed-3.png 480w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051524\/unnamed-3-300x183.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/p>\n<p><strong>Result:<\/strong> greet is available to instances of MyClass<\/p>\n<h3>include Foo Outside a Class<\/h3>\n<p>When you use include outside any class, it adds the module Foo to Object&#8217;s ancestors. Since all objects inherit from Object, the methods in Foo become available for all objects.<\/p>\n<p><strong>Example:<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051639\/unnamed-1-1.png\" alt=\"ouput2\" width=\"484\" height=\"272\" class=\"alignright size-full wp-image-12046\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051639\/unnamed-1-1.png 484w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/18051639\/unnamed-1-1-300x169.png 300w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/p>\n<p><strong>Result:<\/strong> greet is now available for all objects (like String, Array, etc.)<\/p>\n<h2>When to Use Each Approach<\/h2>\n<p><strong>Inside the class:<\/strong> Use this when you want the module\u2019s methods to be available only in specific classes.<br \/>\n<strong>Outside the class:<\/strong> Use this when you want the module\u2019s methods to be available globally across all objects.<\/p>\n<h2>Conclusion<\/h2>\n<ul>\n<li>Inside a class: Methods are added to a specific class.<\/li>\n<li>Outside a class: Methods are added globally to all objects.<\/li>\n<\/ul>\n<p><strong>While using include inside a class is more common and safer, using it outside can be helpful for adding shared behavior across all objects.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s take an example to better understand the differences. include Foo Inside a Class 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&#8217;s methods. Example: Result: greet is available to instances of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12044,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-12043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12043"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12043"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12043\/revisions"}],"predecessor-version":[{"id":12047,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12043\/revisions\/12047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12044"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}