The Wayback Machine - https://web.archive.org/web/20240710053611/https://www.geeksforgeeks.org/include-v-s-extend-in-ruby/
Open In App

Include v/s Extend in Ruby

Last Updated : 10 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Ruby program of Include and Extend
 
# Creating a module contains a method
module Geek
  def geeks
    puts 'GeeksforGeeks!'
  end
end
  
class Lord
 
  # only Lord can access geek methods
  # with the instance of the class.
  include Geek
end
  
class Star
   
  # only Lord 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




# Ruby program to understand include and extend
 
# Creating a module contains a method
module Geek
  def prints(x)
    puts x
  end
end
  
class GFG
 
  # by using both include and extend
  # we can access them by both instances
  #  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!!


Previous Article
Next Article

Similar Reads

Ruby | String include? Method
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str.include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false. Example 1: # Ruby program to demonstrate # the include? method #
1 min read
Ruby | Hash include?() function
Hash#include?() is a Hash class method which checks whether the given key is present in hash. Syntax: Hash.include?() Parameter: Hash values Return: true - if given key"" is present in hash otherwise return false Example #1 : # Ruby code for Hash.has_value?() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300,
2 min read
Ruby | Set include?() function
The include?() is an inbuilt method in Ruby returns true if the set contains the given object. It returns false if it does not contains the given object. Syntax: s1_name.include?(object) Parameters: The function accepts a mandatory parameter object whose presence is to be checked for. Return Value: It returns true if the set contains the given obje
1 min read
Ruby | Array class include?() operation
Array#include?() : include?() is a Array class method checks if the argumented object is present in the array or not. Syntax: Array.include?() Parameter: obj - element to find Return: true - if the element is present; otherwise false Code #1 : Example for include?() method # Ruby code for include?() method # declaring array a = [18, 22, 33, nil, 5,
1 min read
Ruby | Enumerable include?() function
The include?() of enumerable is an inbuilt method in Ruby returns a boolean value. It returns true if the enumerable contains the given elements, or else it returns false. Syntax: enu.include?(el) Parameters: The function takes an element which is to be checked for. Return Value: It returns a boolean value. Example #1: # Ruby program for include? m
1 min read
Ruby | Range include?() function
The include?() is an inbuilt method in Ruby returns a boolean value true if the given object lies within the given range, else it returns false. Syntax: range1.include?(obj) Parameters: The function accepts an object which is to be checked for. Return Value: It returns a boolean value true if the given object lies within the given range, else it re
1 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Private Classes in Ruby
The concept of private, protected and public methods in Ruby is a bit different than it other languages like Java. In Ruby, it is all about which class the person is calling, as classes are objects in ruby. Private Class When a constant is declared private in Ruby, it means this constant can never be called with an explicit receiver, a private cons
3 min read
Ruby | Matrix cofactor() function
The cofactor() is an inbuilt method in Ruby returns the cofactor of the given index. Syntax: mat1.cofactor(i, j) Parameters: The function needs two index i and j, whose cofactor is to be returned. Return Value: It returns the cofactor of the given index. Example 1: # Ruby program for cofactor() method in Matrix # Include matrix require "matrix
1 min read
Ruby Integer odd? function with example
The odd? function in Ruby returns a boolean value. It returns true if the number is odd, else it returns false. Syntax: number.odd? Parameter: The function takes the integer which is to be checked for odd or not. Return Value: The function returns a boolean value which determines if the value is odd or not. Example #1: # Ruby program of integer odd
1 min read
Article Tags :
three90RightbarBannerImg