<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: neha-saggam</title>
    <description>The latest articles on DEV Community by neha-saggam (@nehasaggam).</description>
    <link>https://dev.to/nehasaggam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626116%2Fce9d93ff-7912-477e-a8d1-15081c2daf46.jpeg</url>
      <title>DEV Community: neha-saggam</title>
      <link>https://dev.to/nehasaggam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nehasaggam"/>
    <language>en</language>
    <item>
      <title>RoR - extend, &lt; (inheritance), include - know the difference</title>
      <dc:creator>neha-saggam</dc:creator>
      <pubDate>Wed, 06 Dec 2023 16:21:25 +0000</pubDate>
      <link>https://dev.to/nehasaggam/ror-extend-inheritance-include-know-the-difference-1hpa</link>
      <guid>https://dev.to/nehasaggam/ror-extend-inheritance-include-know-the-difference-1hpa</guid>
      <description>&lt;p&gt;While going through RoR code I saw &lt;code&gt;extend, includes and &amp;lt;&lt;/code&gt;. Now for any newbie it is difficult to understand the differences between these. Hence let us understand with a few examples,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&amp;nbsp;&lt;br&gt;
Is used for inheritance&lt;br&gt;
For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Parent
  def some_method
    puts "Hello from Parent!"
  end
end

class Child &amp;lt; Parent
end

my_obj = Child.new
my_obj.some_method #Output: Hello from Parent!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we see that the class Child is able to use some_method defined in the Parent class i.e it inherits the behavior of a parent. This maintains an "is_a" relationship.&lt;br&gt;
Now a child can override a parent's behavior and instead have its own behavior,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Parent
  def some_method
    puts "Hello from Parent!"
  end
end

class Child &amp;lt; Parent
  def some_method
    puts "Hello from Child!"
  end
end

my_obj = Child.new
my_obj.some_method #Output: Hello from Child!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;extend&lt;/code&gt;&lt;br&gt;
This keyword gives you the ability to use the class level or module level methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module SomeModule
  def some_method
    puts "Hello from SomeClass!"
  end
end

class Child
  extend SomeModule
end

Child.some_method #Output: Hello from SomeClass!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;include&lt;/code&gt;&lt;br&gt;
include allows you to mix other class functionalities into the included class without having an "is_a" relationship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeClass
  def some_method
    puts "Hello from SomeClass!"
  end
end

class Main
  include SomeClass
end

my_obj = Main.new
my_obj.some_method #Output: Hello from SomeClass!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are still able to override the method in the main class,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeClass
  def some_method
    puts "Hello from SomeClass!"
  end
end

class Main
  include SomeClass

  def some_method
    puts "Hello from Main!"
  end
end

my_obj = Main.new
my_obj.some_method #Output: Hello from Main!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this explains the difference between &amp;lt;, extend and include.&lt;/p&gt;

</description>
      <category>ror</category>
      <category>ruby</category>
      <category>rails</category>
      <category>inheritance</category>
    </item>
  </channel>
</rss>
