
Is module_function really the same as extend self?
A deep dive into `module_function` and `extend self`
In this article, we’re going to explore the following topics:
- difference between
module_function
andextend self
module_function
vsextend self
If you’re not familiar with the notion of module in Ruby: Module in Ruby Part I
Before to start
I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1
Please feel free to spread the word and share this link! 🙏
Thank you for your time!
Introduction
In Ruby, a module can be used as logical entity. It groups methods at a module level without using the mixin facility — for example, the Base64
module (I highly recommend you to read the source code).
In Ruby, there is a set of techniques to achieve this expected result. The 2 best known are Module#module_function
and extend self
.
How it works
Let’s detail what are the main differences between these 2 approaches
extend self
By extending self
within the RubyCademy
module, all the instance methods defined within this module are now available at a module level — That’s why we can call RubyCademy.headline
.
module_function
Here, we also define a RubyCademy#headline
instance method. Then we call module_function :headline
. At this moment the headline
method is also available as module method — also known as module function.
extend self vs module_function
Until now, we’ve seen 2 ways to declare instance methods as module methods: extend self
and module_function
. So, let’s see what happens behind the scene in order to really understand their differences.
Method visibility
First, we define a TheDevelopersJourney
module that contains a headline
instance method. Then we generate a headline
module function by using the extend self
mechanism. Next, we define a MediumBlog
class that includes our module. Finally we notice that our call to extend self
somewhat changed the type of methods:
- a new
headline
singleton method has been added toTheDevelopersJourney
module - the
headline
included method is public
Ok, now what about module_function
?
First, we define a RubyCademy
module that contains a headline
instance method. Then we generate a headline
module function by using the module_function
routine. Next, we define a Website
class that includes our module. Finally we notice that our call to module_function
somewhat changed the type of methods:
- a new
headline
singleton method has been added toRubyCademy
module - the
headline
included method is now private
This is the main difference between extend self
and module_function
. The latter one really creates a module function by restricting the access to the included method while extend self
still allows the access to the included method.
module function copy
Another difference remains in the facts that extend self
and module_function
don’t generate the same kind of module function
First, we define the :who_am_i
method as module function using module_function :who_am_i
. Then we override this method but we notice that our modification is not applied. Finally, to apply our modification, we must call module_function :who_am_i
again.
Indeed, as our module function is a copy of the original who_am_i
instance method, our modification of this instance method is naturally not propagated to the module function — unless we redeclare the modified instance method as module function using module_function
.
Not let’s see how extend self
works with overridden instance methods
First, we define the :who_am_i
method as module function using extend self
. Then we override the who_am_i
instance method and we notice that our modification is applied to the module function.
Indeed, as our module function is not a copy of the original who_am_i
instance method, our modification of the instance method is naturally propagated to the module function.
Advantages
extend self
module_function
Conclusion
Now you have all the tools to take the right decision when it comes to create a module function.. or not. 😊
Finally, it’s interesting to use this approach when your methods are independent of internal objects — For example, the Math
module.
Note that
module_function
is highly used within the Ruby Standard Library.
Voilà!