Include is used to importing module code. Ruby will throw an error when we try to access the methods of import module with the class directly because it gets imported as a subclass for the superclass. So, the only way is to access it through the instance of the class.
Extend is also used to importing module code but extends import them as class methods. Ruby will throw an error when we try to access methods of import module with the instance of the class because the module gets import to the superclass just as the instance of the extended module. So, the only way is to access it through the class definition.
In simple words, the difference between include and extend is that ‘include’ is for adding methods only to an instance of a class and ‘extend’ is for adding methods to the class but not to its instance.
Example :
# Ruby program of Include and Extend # Creating a module contains a method module Geek def geeks puts 'GeeksforGeeks!' endend class Lord # only can access geek methods # with the instance of the class. include Geek end class Star # only can access geek methods # with the class definition. extend Geek end # object access Lord.new.geeks # class access Star.geeks # NoMethodError: undefined method # `geeks' for Lord:Class Lord.geeks |
Output
GeeksforGeeks! GeeksforGeeks! main.rb:20:in `': undefined method `geeks' for Lord:Class (NoMethodError)
If we want to import instance methods on a class and its class methods too. We can ‘include’ and ‘extend’ it at the same time.
Example :
# Ruby program to understand include and extend # Creating a module contains a method module Geek def prints(x) puts x endend class GFG # by using both include and exclude # we can access them by both instance # and class name. include Geek extend Geek end # access the prints() in Geek # module by include in Lord class GFG.new.prints("Howdy") # object access # access the prints() in Geek # module by extend it in Lord class GFG.prints("GeeksforGeeks!!") # class access |
Output
Howdy GeeksforGeeks!!
Recommended Posts:
- @include vs @extend in SASS
- Ruby | String include? Method
- Ruby | Hash include?() function
- Ruby | Set include?() function
- Ruby | Array class include?() operation
- Ruby | Range include?() function
- Ruby | Enumerable include?() function
- What are .extend and .prototype used for?
- How to Extend an Object in JavaScript ?
- How to include one CSS file in another?
- How to include() all PHP files from a directory ?
- What is the best way to include CSS file? Why use @import?
- AngularJS | ng-include Directive
- How to include a JavaScript file in another JavaScript file ?
- Why does AngularJS include an empty option in select ?
- How to include inline JavaScript in HAML ?
- How to Include Factors in Regression using R Programming?
- How to include a font .ttf using CSS ?
- Private Classes in Ruby
- Ruby | Matrix cofactor() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

