๐๐ผ๐ฒ๐ module_function ๐ฟ๐ฒ๐ฎ๐น๐น๐ ๐๐ต๐ฒ ๐๐ฎ๐บ๐ฒ ๐ฎ๐ 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
Introduction
In Ruby, a module can be used as a logical entity. It groups methods at a module level without using the mixin facility โ for example, the Base64
module (I highly recommend you 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 RubyCademy#headline
. 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 scenes in order to really understand their differences.
Method visibility
First, we define a moduleTheDevelopersJourney
that contains a headline
instance method. Then we generate a headline
module function by using the extend self
mechanism.
Next, we define a class MediumBlog
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 included
headline
method is public
Okay, now what about module_function
?
First, we define a moduleRubyCademy
that contains a headline
instance method. Then we generate a headline
module function by using the module_function
routine.
Next, we define a class Website
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 included
headline
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 access to the included method while extend self
still allows access to the included method.
module function copy
Another difference remains in the fact that extend self
and module_function
donโt generate the same kind of module function
First, we define the :who_am_i
method as a 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, the modification of this instance method is naturally not propagated to the module function โ unless we redeclare the modified instance method as a 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 a 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 make the right decision when it comes to creating 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.
Ruby Mastery
Weโre currently finalizing our first online course: Ruby Mastery.
Join the list for an exclusive release alert! ๐
๐ Ruby Mastery by RubyCademy
Also, you can follow us on x.com as weโre very active on this platform. Indeed, we post elaborate code examples every day.
๐