<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomer Kimia</title>
    <description>The latest articles on DEV Community by Tomer Kimia (@tkimia).</description>
    <link>https://dev.to/tkimia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F226780%2Ffb9bc639-9883-4183-bc6f-9b2dac36f036.jpg</url>
      <title>DEV Community: Tomer Kimia</title>
      <link>https://dev.to/tkimia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tkimia"/>
    <language>en</language>
    <item>
      <title>4 Ways to Write Module Functions</title>
      <dc:creator>Tomer Kimia</dc:creator>
      <pubDate>Thu, 26 Sep 2019 14:20:00 +0000</pubDate>
      <link>https://dev.to/tkimia/4-ways-to-write-module-functions-1lmm</link>
      <guid>https://dev.to/tkimia/4-ways-to-write-module-functions-1lmm</guid>
      <description>&lt;p&gt;This is not a best practices post.&lt;/p&gt;

&lt;p&gt;Let's say that you wanted to add some functions that operated on strings, but didn't want to monkeypatch the String class. Let's also say that you take inspiration from Apache Commons' Lang library and wanted to create your own version of &lt;a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html"&gt;StringUtils&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We want to end up with functions can be called like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;StringUtils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alphanumeric?&lt;/span&gt; &lt;span class="s2"&gt;"Hello123"&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;span class="no"&gt;StringUtils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alphanumeric?&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt; &lt;span class="c1"&gt;# false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;StringUtils&lt;/code&gt; is not an object that we'd want to instantiate, we'll write it as a module.&lt;/p&gt;

&lt;p&gt;Now you have to make choice: how do we want to write the code so that the module itself can be the receiver for these methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 1: Prepend &lt;code&gt;self&lt;/code&gt; to the method name
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;StringUtils&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alphanumeric?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="sr"&gt;/\A[0-9a-zA-Z]+\z/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match?&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is equivalent to &lt;code&gt;def StringUtils.alphanumeric?&lt;/code&gt;, which is an example hosted on the &lt;a href="https://ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/book/tut_modules.html"&gt;official Ruby docs website&lt;/a&gt;. However, Rubocop will complain and suggest that you switch to &lt;code&gt;self.alphanumeric?&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 2: Put them in the "Singleton Class" (Eigenclass)
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;StringUtils&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;alphanumeric?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="sr"&gt;/\A[0-9a-zA-Z]+\z/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match?&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I was thoroughly confused when I first read this type of code. Plus, if you search &lt;em&gt;Ruby Singleton class&lt;/em&gt; and you'll find dozens of unrelated hits about implementing the singleton pattern. Good thing we came up with the much clearer name: "eigenclass".&lt;/p&gt;

&lt;p&gt;Anyway, this option is nice because you can group all of the methods you want the module to receive inside a block. For that reason, I often see this option used inside of classes, alongside instance methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 3: &lt;code&gt;extend self&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;StringUtils&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;alphanumeric?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="sr"&gt;/\A[0-9a-zA-Z]+\z/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match?&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I saw this in the &lt;a href="https://github.com/pact-foundation/pact-ruby/blob/master/spec/support/spec_support.rb"&gt;Pact source code&lt;/a&gt; and was taken aback.&lt;/p&gt;

&lt;p&gt;This code allows you to call &lt;code&gt;StringUtils.alphanumeric?&lt;/code&gt;, but it also allows you to &lt;code&gt;include StringUtils&lt;/code&gt; in some other class.&lt;/p&gt;

&lt;p&gt;I have a hard time thinking of a use case in which you'd want that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 4: &lt;code&gt;module_function&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;StringUtils&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;alphanumeric?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="sr"&gt;/\A[0-9a-zA-Z]+\z/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match?&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All methods defined under the &lt;code&gt;module_function&lt;/code&gt; line can be received by the module itself, AND will be made private so they can't be included in classes. You can also put &lt;code&gt;module_function&lt;/code&gt; on the bottom of you module and give it symbol arguments so that you can pick and choose the affected methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;StringUtils&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;alphanumeric?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="sr"&gt;/\A[0-9a-zA-Z]+\z/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match?&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;module_function&lt;/span&gt; &lt;span class="ss"&gt;:alphanumeric?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I won't say which option I'd choose to write this code. While there are nuanced arguments for each option, they all get the job done. I started this post by saying that it's not about best practice.&lt;/p&gt;

&lt;p&gt;It's often hard to wrap your head around the patterns used in code you haven't written yourself. This is especially notable when someone writes code that you would have written differently.  I hope that seeing these snippets side by side will help you recognize what's going on when reading other people's code in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus&lt;/strong&gt;&lt;br&gt;
Here's a fun fact: If you type &lt;code&gt;extend self&lt;/code&gt; while using Rubocop, it'll instruct you to use &lt;code&gt;module_method&lt;/code&gt; instead. However, these are clearly not the same! &lt;a href="https://github.com/rubocop-hq/ruby-style-guide/issues/556"&gt;More here.&lt;/a&gt; &lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
  </channel>
</rss>
