Is module_function really the same as extend self?

A deep dive into `module_function` and `extend self`

Mehdi Farsi
Jul 4, 2020 · 3 min read

In this article, we’re going to explore the following topics:

  • difference between module_function and extend self
  • module_function vs extend self

If you’re not familiar with the notion of module in Ruby: Module in Ruby Part I

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

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.

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.

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 to TheDevelopersJourney 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 to RubyCademy 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.

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à!

RubyCademy

E-Learning platform for Ruby and Ruby on Rails

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store