๐——๐—ผ๐—ฒ๐˜€ module_function ๐—ฟ๐—ฒ๐—ฎ๐—น๐—น๐˜† ๐˜๐—ต๐—ฒ ๐˜€๐—ฎ๐—บ๐—ฒ ๐—ฎ๐˜€ extend self?

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

Tech - RubyCademy
RubyCademy
Published in
3 min readJul 4, 2020

--

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

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

๐Ÿ’š

--

--