What is the difference between include and extend in Ruby?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What is the difference between include and extend in Ruby?

Include vs Extend: Demystifying Ruby Mixins

<p>🤔 Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me. </p>

<p>So, what is the difference between include and extend in Ruby? Let's break it down! 🕵️‍♀️</p>

The Basics: Include and Extend

<ul> <li><strong>include</strong>: mixes in specified module methods as <strong>instance methods</strong> in the target class</li> <li><strong>extend</strong>: mixes in specified module methods as <strong>class methods</strong> in the target class</li> </ul>

<p>Essentially, when you include a module in a class, you are adding the module's methods as instance methods in that class. On the other hand, when you extend a class with a module, you are adding the module's methods as class methods in that class. 🎭</p>

🐉 A Bigger Dragon?

<p>Now, you may wonder if there's a bigger, hidden dragon behind this apparent simplicity. Let's find out! 🔍</p>

<pre><code>module ReusableModule def module_method puts "Module Method: Hi there!" end end class ClassThatIncludes include ReusableModule end class ClassThatExtends extend ReusableModule end puts "Include" ClassThatIncludes.new.module_method # "Module Method: Hi there!" puts "Extend" ClassThatExtends.module_method # "Module Method: Hi there!" </code></pre>

<p>As you can see from the code snippet above, when we include <code>ReusableModule</code> in <code>ClassThatIncludes</code>, we can directly call <code>module_method</code> on an instance of <code>ClassThatIncludes</code>.</p>

<p>Similarly, when we extend <code>ClassThatExtends</code> with <code>ReusableModule</code>, we can call <code>module_method</code> directly on the class itself, without creating any instances. 🎉</p>

Conclusion

<p>And that's the difference between include and extend in Ruby! 🎈</p>

<p>Use include when you want to add module methods as instance methods in a class, and extend when you want to add module methods as class methods in a class.</p>

<p>With this knowledge in hand, you can now confidently wield the power of mixins in your Ruby code. Happy coding! 💻</p>

<p>Got any other Ruby questions? Want to share your mixin adventures? Leave a comment below and let's start a conversation! 🗣️</p>

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my