<?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>Forem: abbiecoghlan</title>
    <description>The latest articles on Forem by abbiecoghlan (@abbiecoghlan).</description>
    <link>https://forem.com/abbiecoghlan</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%2F552088%2F2d1e1c2e-1afd-4ae9-b406-6498bf9d25ee.jpeg</url>
      <title>Forem: abbiecoghlan</title>
      <link>https://forem.com/abbiecoghlan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abbiecoghlan"/>
    <language>en</language>
    <item>
      <title>Semantic HTML for improved accessibility and SEO</title>
      <dc:creator>abbiecoghlan</dc:creator>
      <pubDate>Mon, 07 Jun 2021 01:44:33 +0000</pubDate>
      <link>https://forem.com/abbiecoghlan/benefits-of-semantic-html-1fc4</link>
      <guid>https://forem.com/abbiecoghlan/benefits-of-semantic-html-1fc4</guid>
      <description>&lt;h2&gt;
  
  
  What is a Semantic HTML element?
&lt;/h2&gt;

&lt;p&gt;A Semantic HTML element is an element that clearly describes its own meaning through its name. For example, &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; is a semantic html element, and &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is not. When you use &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;, it communicates to the browser that that portion of your website is the header. If you were to use &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; instead, although your content might &lt;em&gt;look&lt;/em&gt; the same to many users, you haven’t communicated to the browser any information about the fact that it is your website’s header.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;There are many semantic HTML elements. Some of the most common semantic elements include: &lt;br&gt;
&lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;: a header element is used for the web page header. A header may include a logo, title for the page, and site navigation. &lt;br&gt;
&lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt;: A nav element includes links to other pages of the website, or links to anchors on the same page. It is often placed inside of a header or footer element. &lt;br&gt;
&lt;code&gt;&amp;lt;footer&amp;gt;&lt;/code&gt;: a footer element may contain nav links, links to contact information, and information about the ownership of the website.&lt;br&gt;
&lt;code&gt;&amp;lt;main&amp;gt;&lt;/code&gt; Main is an element that is used to hold the main content of the page, and should be used for information that is unique to that page. &lt;br&gt;
&lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt;: The article element is used for content that is a self-contained and independent part of a website. Information in this section should be able to stand alone and make sense without any additional context. It is used for things like blog posts, newspaper articles, or forum posts. &lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Semantic Elements
&lt;/h2&gt;

&lt;p&gt;Semantic HTML elements don’t take any longer to write than non-semantic elements, and using semantic elements provide many benefits. Semantic elements provide information about how your document is organized and what each element contains. When you use a semantic element, you are communicating more specific information about the document to the browser. This is important for accessibility as well as SEO. When you use a semantic element, you improve your website’s accessibility for users across the world, especially for users who use screen readers. Without semantic elements, screen readers have less information to utilize to adapt content so that users can access information accurately. Search engines give more importance to key words inside semantic elements, so your website will be easier to find than it would be if you used non-semantic elements. If you use semantic elements, you also have the option to use them as hooks for CSS styling without needed to assign additional class names or IDs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;p&gt;Many people utilize non-semantic elements or misuse elements in order to achieve styling effects, rather than using css to achieve the same effect. Here are some common mistakes and solutions:&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Mistake 1: using a block quote when you want content indented
&lt;/h3&gt;

&lt;p&gt;Many developers will us the block quote element when they want content indented, as it is a quick way to achieve that styling. If information inside of a block quote is not an actual piece of quoted information, you should not use it. Rather, you should use a different, more accurate element, and use CSS to style the margins to achieve the same impact. &lt;/p&gt;

&lt;h3&gt;
  
  
  Common Mistake 2: using a p tag to add extra space
&lt;/h3&gt;

&lt;p&gt;Another common misuse of elements is the use of a &lt;/p&gt;
&lt;p&gt; tag to add extra space between elements. Again, this is done because it is a quick and easy way to achieve a desired visual styling. However, this communicates misinformation about the content of that section to your browser. Again, the solution here is to utilize css to add margins and padding as needed for the same visual impact. &lt;/p&gt;

&lt;h3&gt;
  
  
  Common Mistake 3: misuse of header tags
&lt;/h3&gt;

&lt;p&gt;h1-h6 tags are often chosen based on a desire font size. However, this again miscommunicates information to the browser for screen readers and search engines. Rather than choosing a heading tag based on font size, tags should be chosen based on content. An h1 tag is particularly important for SEO, and should be the main title of the page, communicating the main idea of what the page is about. An accurate h1 tag will make your website much more accessible by search. In general, header tags should convey a hierarchy of importance. If you list out out your header tags, you should have a sort of “outline” of your website. If you want the styling or size of these elements to change, you should utilize css to target font-size and font-weight properties rather than changing the tag. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Semantic HTML tags are important and beneficial for accessibility and SEO. They provide more information about the organization of your website and its content to search engines and screen readers.  It is easy to utilize semantic HTML if you keep it in mind from the start of your project. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ruby Modules: include vs extend vs prepend</title>
      <dc:creator>abbiecoghlan</dc:creator>
      <pubDate>Wed, 20 Jan 2021 17:24:49 +0000</pubDate>
      <link>https://forem.com/abbiecoghlan/ruby-modules-include-vs-extend-vs-prepend-4gmc</link>
      <guid>https://forem.com/abbiecoghlan/ruby-modules-include-vs-extend-vs-prepend-4gmc</guid>
      <description>&lt;p&gt;Previously, we learned about the differences between class inheritance and modules in Ruby. You can read more about that &lt;a href="https://dev.to/abbiecoghlan/class-inheritance-vs-modules-in-ruby-1fha"&gt;here&lt;/a&gt;. In that post, we reviewed how to use modules to share functionality between classes that do not have a clear hierarchical arrangement. Specifically, we used the &lt;code&gt;include&lt;/code&gt; keyword to give our classes access to instance methods. In this post, we will focus on digging deeper into modules in Ruby by reviewing the use of the &lt;code&gt;include&lt;/code&gt; keyword and introducing two other keywords, &lt;code&gt;extend&lt;/code&gt; and &lt;code&gt;prepend&lt;/code&gt;. Understanding the different behaviors of each of these three keywords and knowing when to use them will provide us with the power to utilize modules to extend functionality to our classes in a variety of ways. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;include&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;include&lt;/code&gt; is the keyword that we will use within our classes to provide access to the methods defined in a module as &lt;em&gt;instance&lt;/em&gt; methods in our class. In order to do so, we will first define a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;IndoorPet&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, all we have to do to give our classes access to the instance method &lt;code&gt;can_be_housebroken?&lt;/code&gt; as it is defined in our IndoorPet module is add &lt;code&gt;include IndoorPet&lt;/code&gt; in the body of our class when we define it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rabbit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;span class="n"&gt;roger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Roger"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;roger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; 

&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;NoMethodError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;undefined&lt;/span&gt; &lt;span class="nb"&gt;method&lt;/span&gt; &lt;span class="sb"&gt;`can_be_housebroken?' for Rabbit:Class


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, we have now &lt;em&gt;included&lt;/em&gt; the module's instance method &lt;code&gt;can_be_housebroken?&lt;/code&gt; in our Dog, Cat, and Rabbit classes. Thus, any instances of those classes will now have access to the &lt;code&gt;can_be_housebroken?&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;When you &lt;code&gt;include&lt;/code&gt; a module, Ruby will insert the module into the class's ancestry chain just above our class, between our class and it's superclass. This chain can be seen when calling &lt;code&gt;.ancestors&lt;/code&gt; on our Rabbit class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;extend&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we know how to use include to provide a class with access to a module's instance methods, you might be wondering how to provide a class with access to a module's &lt;em&gt;class&lt;/em&gt; methods. The &lt;code&gt;extend&lt;/code&gt; keyword does just that. It is used in exactly the same way as include, except that instead of instance methods which can be called on individual instances of our class, all of the methods we are &lt;em&gt;extending&lt;/em&gt; to our class will be provided as class methods we can call on the class as a whole. &lt;/p&gt;

&lt;p&gt;Instead of including &lt;code&gt;can_be_housebroken?&lt;/code&gt; as instance method, let's try extending it as a class method. First, we define our module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;IndoorPet&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we use the &lt;code&gt;extend&lt;/code&gt; keyword the same way we used the &lt;code&gt;include&lt;/code&gt; keyword previously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
    &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rabbit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;



&lt;span class="n"&gt;roger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Roger"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;roger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;NoMethodError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;undefined&lt;/span&gt; &lt;span class="nb"&gt;method&lt;/span&gt; &lt;span class="sb"&gt;`can_be_housebroken?' for  #&amp;lt;Rabbit:0x00007fa84389c748&amp;gt; 

Rabbit.can_be_housebroken?
=&amp;gt; true

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, we have now &lt;em&gt;extended&lt;/em&gt; the module's class method &lt;code&gt;can_be_housebroken?&lt;/code&gt; to our Dog, Cat, and Rabbit classes. Now those classes as a whole have access to the &lt;code&gt;can_be_housebroken?&lt;/code&gt; method. Note that you can no longer &lt;code&gt;call can_be_housebroken?&lt;/code&gt; on an individual instance of the class. With &lt;code&gt;extend&lt;/code&gt;, we have only given the method to the class as a class method. &lt;/p&gt;

&lt;p&gt;When you extend a module, ruby will provide the module's methods to the class as class methods. However, unlike with &lt;code&gt;include&lt;/code&gt;, when you &lt;code&gt;extend&lt;/code&gt; a module, Ruby will not insert the module into the class's ancestry chain. We can see this by calling &lt;code&gt;.ancestors&lt;/code&gt; on our Rabbit class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;prepend&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The third keyword for providing a module's methods to a class is &lt;code&gt;prepend&lt;/code&gt;. We can use &lt;code&gt;prepend&lt;/code&gt; in the same way we would use &lt;code&gt;include&lt;/code&gt; or &lt;code&gt;extend&lt;/code&gt; by first defining the module and then using the keyword and name of the module within the class's definition, as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;IndoorPet&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;  


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
    &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rabbit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In many ways, &lt;code&gt;prepend&lt;/code&gt; operates like &lt;code&gt;include&lt;/code&gt;. Both provide a class access to a module's methods as instance methods. Therefore, we can call the imported methods that we have prepended from our module on specific instances of a class, and not on the class itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;roger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Roger"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;roger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; 

&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
&lt;span class="no"&gt;NoMethodError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;undefined&lt;/span&gt; &lt;span class="nb"&gt;method&lt;/span&gt; &lt;span class="sb"&gt;`can_be_housebroken?' for Rabbit:Class
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the difference between &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;prepend&lt;/code&gt; is related to the location in the ancestry chain where the module is placed. With &lt;code&gt;prepend&lt;/code&gt;, the module is not inserted between the class and it's superclass as it was with &lt;code&gt;include&lt;/code&gt;. It is actually inserted at the very bottom of the ancestry chain, as seen below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;IndoorPet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Rabbit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The importance of this distinction will be explored further in our next post! &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;include&lt;/code&gt; vs &lt;code&gt;extend&lt;/code&gt; vs &lt;code&gt;prepend&lt;/code&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;include&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Provides a class with access to a module's methods as instance methods&lt;/li&gt;
&lt;li&gt;Allows the methods to be called on individual instances of the class
&lt;/li&gt;
&lt;li&gt;Does not allow methods to be called on the class as a whole&lt;/li&gt;
&lt;li&gt;Inserts module into ancestry chain between the class and superclass &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;extend&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Provides a class with access to a module's methods as class methods &lt;/li&gt;
&lt;li&gt;Does not allow the methods to be called on individual instances of the class
&lt;/li&gt;
&lt;li&gt;Allows methods to be called on the class as a whole&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;prepend&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Provides a class with access to a module's methods as instance methods &lt;/li&gt;
&lt;li&gt;Allows the methods to be called on individual instances of the class
&lt;/li&gt;
&lt;li&gt;Does not allow methods to be called on the class as a whole&lt;/li&gt;
&lt;li&gt;Inserts module at the bottom of the ancestry chain&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>modules</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Class Inheritance VS Modules in Ruby</title>
      <dc:creator>abbiecoghlan</dc:creator>
      <pubDate>Mon, 04 Jan 2021 18:53:17 +0000</pubDate>
      <link>https://forem.com/abbiecoghlan/class-inheritance-vs-modules-in-ruby-1fha</link>
      <guid>https://forem.com/abbiecoghlan/class-inheritance-vs-modules-in-ruby-1fha</guid>
      <description>&lt;p&gt;In Ruby, we can use either class inheritance or modules to provide shared functionality throughout a program. This post explores the difference between class inheritance and modules, and considerations to help determine the appropriate use of each.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance between classes in Ruby allows for the creation of classes that have access to shared methods while still maintaining distinct, unique classes. When one class, known as the child or subclass, is inherited from another class, known as the parent or “super” class, it is given access to all of the methods of the parent. For example, we might have a parent class “Dog”, with a child class of “Dalmatian”. In the code below, we define these classes without the use of inheritance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bark&lt;/span&gt;
        &lt;span class="s2"&gt;"Woof!"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;
        &lt;span class="s2"&gt;"zzzzz"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_that_smell&lt;/span&gt;
        &lt;span class="s2"&gt;"sniff sniff sniff"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dalmatian&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bark&lt;/span&gt;
        &lt;span class="s2"&gt;"Woof!"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;
        &lt;span class="s2"&gt;"zzzzz"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_that_smell&lt;/span&gt;
        &lt;span class="s2"&gt;"sniff sniff sniff"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The acronym DRY stands for "don't repeat yourself", and it is an important principle in software design. In the code below, using class inheritance, we can get the same functionality as above, but we have the benefit of keeping our code DRY. Our Dalmatian class now has access to all of the behavior of the Dog class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bark&lt;/span&gt;
        &lt;span class="s2"&gt;"Woof!"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;
        &lt;span class="s2"&gt;"zzzzz"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_that_smell&lt;/span&gt;
        &lt;span class="s2"&gt;"sniff sniff sniff"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dalmatian&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Dog&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The use of inheritance allows us to share methods between classes in a way that reflects the real-world hierarchical relationship between our classes, and provides shared functionality accordingly. If a dog can bark, a Dalmatian can bark.&lt;/p&gt;

&lt;p&gt;An important note about class inheritance when considering use is that, although you can inherit from a class that inherits from another class that inherits from yet another class- a single class can only inherit from &lt;em&gt;one class at a time&lt;/em&gt; when it is defined.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules
&lt;/h2&gt;

&lt;p&gt;Let's imagine we wanted to define an instance method that returns true if the animal is able to be housebroken. We could define this as an instance method within our dog class and have our individual dog breeds inherit the method. But what if our program had other animal classes, such as cats and rabbits, that we wanted to provide this method to as well?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rabbit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above, we would have to define those methods over and over again. Sometimes, as in this case, we might want to share functionality between classes that do not have a clear hierarchical arrangement. Instead of making a new class that is built from the blueprint of an existing class, like when we inherit, we may simply want to group methods together and make those methods available to a variety of other classes. Modules allow us to do just that.&lt;/p&gt;

&lt;p&gt;Let’s use a module instead of class inheritance. First, we will define our module. Then, within the definition of our classes, we can use the keyword &lt;em&gt;include&lt;/em&gt; along with the module name in any number of classes in which we would like to provide access to the functions we defined in our module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;IndoorPet&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_be_housebroken?&lt;/span&gt;
        &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;  

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; 
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; 

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rabbit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt; 
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;IndoorPet&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the use of Modules provides a great solution for extending functionality and avoiding repetitive code in situations where there is no clear parent-child relationship. Once it is defined, a module can be included in many classes. There is also no limit to how many modules you can include in an individual class. One thing to keep in mind about modules when considering use is that, unlike with classes, you cannot have an &lt;em&gt;instance&lt;/em&gt; of a module. &lt;/p&gt;

&lt;h1&gt;
  
  
  Class Inheritance vs Modules
&lt;/h1&gt;

&lt;p&gt;Class inheritance and modules both allow us to extend functionality throughout our program while reducing repetitive code. When trying to figure out when to use modules vs class inheritance, some considerations include whether or not you need to create instances, whether or not there is a hierarchical relationship between classes, and how the extended functionality is related to the relationships between groups. If you are trying to build a class from the framework of an already defined class, you can use class inheritance to adopt the behaviors of a parent class. If you've created a group of methods that you’d like to use repeatedly throughout your program in many different classes that are not hierarchically related, you can use modules to provide your classes access to the methods defined within your modules.&lt;/p&gt;

&lt;p&gt;In our next post, we dig deeper into modules in ruby, exploring the &lt;code&gt;include&lt;/code&gt;, &lt;code&gt;extend&lt;/code&gt;, and &lt;code&gt;prepend&lt;/code&gt; keywords. You can read that post &lt;a href="https://dev.to/abbiecoghlan/ruby-modules-include-vs-extend-vs-prepend-4gmc"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>inheritance</category>
      <category>modules</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
