<?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: Alvaro Padilla</title>
    <description>The latest articles on DEV Community by Alvaro Padilla (@esevaro).</description>
    <link>https://dev.to/esevaro</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F935809%2Fd71a3d43-8d63-4140-9a59-fa8aed02b374.jpeg</url>
      <title>DEV Community: Alvaro Padilla</title>
      <link>https://dev.to/esevaro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/esevaro"/>
    <language>en</language>
    <item>
      <title>Monkey-patching: Messing with your Ruby methods</title>
      <dc:creator>Alvaro Padilla</dc:creator>
      <pubDate>Tue, 31 Jan 2023 16:47:42 +0000</pubDate>
      <link>https://dev.to/esevaro/monkey-patching-messing-with-your-ruby-methods-id</link>
      <guid>https://dev.to/esevaro/monkey-patching-messing-with-your-ruby-methods-id</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
In Software Development, as in any other profession, knowing your tools and taking the most out of them is a must if you really want to be proficient in what you do. In Ruby, as in some other programming languages, you can take advantage of monkey patching and use it either to add extra functionality to existing code or to create a custom implementation as a workaround that fits the problem you are trying to solve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Monkey patch?&lt;/strong&gt;&lt;br&gt;
One of the etymological definitions of &lt;a href="https://en.wikipedia.org/wiki/Monkey_patch" rel="noopener noreferrer"&gt;monkey patch&lt;/a&gt; is referred to as “monkeying about” with the code, or in other words “messing with it”.&lt;/p&gt;

&lt;p&gt;Monkey patching is a dynamic feature that basically refers to the ability to have classes open all the time, which enables you to modify or extend them at runtime either by adding new methods, overriding them, or even deleting them.&lt;/p&gt;

&lt;p&gt;We can implement it in some dynamic programming languages. Python is the language that first adopted the term ‘monkey patch’, they used to use it with a negative connotation as a way to derogate this technique. Since it wasn’t accepted well at the time, it wasn’t considered a good practice. On the other hand, the Ruby community embraced the term. In some cases, it is also replaced by ‘duck punching’ or ‘functional reloading’, but ‘monkey patching’ remains the most used. Ruby adopted it by giving it a completely different meaning, without understating how dangerous it could be, but now putting a high emphasis on how useful and powerful tool could be to help developers to create great solutions without compromising the integrity and maintainability of their code. The best example of this is the most used and known Ruby framework for web development, Ruby on Rails.&lt;/p&gt;

&lt;p&gt;As with some other controversial topics, there is a discussion out there about this technique and whether or not it is recommended to implement it, and as you may know, opinions are really divided. I’m not going to take a position about it, besides whether it is correct or not to use it, I would say that it really depends on the problem you are trying to solve and its context, so taking the time to analyze it is even more important.&lt;/p&gt;

&lt;p&gt;As dangerous as it may sound it’s also a great tool if used properly, it is so, that you could end up having very elegant and readable solutions when using monkey patches, one of the most popular examples of it is the Rails time methods, e.g. &lt;code&gt;3.days.ago&lt;/code&gt; and so.&lt;/p&gt;

&lt;p&gt;The most common use case of monkey patching is to augment or decorate existing methods. If you find out that monkey patching a class would help you solve a very particular problem on your project, either because doing it might prevent you from completely changing your current implementation, or just because the class in question is a third-party or core class, and after some intents, like even trying to use other gems or other workarounds, you still don’t have what you need, you can make use of it and hopefully end up with a great solution. Of course, there are some considerations and good practices that you can apply. If you want to know a little more about it I suggest reading &lt;a href="https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/" rel="noopener noreferrer"&gt;this post&lt;/a&gt; by Justin Weiss, which would be really helpful on what techniques you can follow in order to monkey patching in a secure way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
In order to understand this technique better, the following are some examples of how we can implement it in different situations depending on our needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a new method&lt;/strong&gt;&lt;br&gt;
Now, let’s try to monkey patch the &lt;code&gt;String&lt;/code&gt; class by adding a new method. Let’s say we need to find out if a string has any number in it, so we’re going to create a new method named &lt;code&gt;has_numbers?&lt;/code&gt; to help us solve this problem. The code needed is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;has_numbers?&lt;/span&gt;
    &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="o"&gt;=~&lt;/span&gt; &lt;span class="sr"&gt;/\d/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"does 'abc' have numbers? &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="s1"&gt;'abc'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has_numbers?&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"does 'abc0123' have numbers? &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="s1"&gt;'abc0123'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has_numbers?&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this code the output should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; does 'abc' have numbers? false
=&amp;gt; does 'abc0123' have numbers? true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it, we’ve monkey patched the String class by adding a new method to it, and now any instance of this class has access to this method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overriding a method&lt;/strong&gt;&lt;br&gt;
This is one of the most common use cases for a monkey patch, overriding the current implementation of an existing method. For this example let’s say that we are required to log and return the custom message &lt;code&gt;this code is under maintenance&lt;/code&gt; each time the &lt;code&gt;length&lt;/code&gt; method of the &lt;code&gt;String&lt;/code&gt; core class is called, instead of returning the string's length. The code would be like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;length&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;under&lt;/span&gt; &lt;span class="n"&gt;maintenance&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the above, we should have the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; this code is under maintenance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Extending an existing method using an Alias&lt;/strong&gt;&lt;br&gt;
Sometimes we need to add some extra functionality to an existing method but we just need it in some places so keeping the original implementation would be required. In those cases, we could extend an existing method by using the alias technique. For this example, let’s say we need to add 2 to the result of calling the &lt;code&gt;size&lt;/code&gt; method of the &lt;code&gt;String&lt;/code&gt; core class. The code would be like the following:&lt;/p&gt;

&lt;p&gt;The code would be like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new_size&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;old_size&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="ss"&gt;:old_size&lt;/span&gt; &lt;span class="ss"&gt;:size&lt;/span&gt;
  &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="ss"&gt;:size&lt;/span&gt; &lt;span class="ss"&gt;:new_size&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"size of '1234'.size is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="s1"&gt;'1234'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the above, we should have the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; size of '1234'.size is 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may be wondering if instead of having two aliases we could just have something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new_size&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="ss"&gt;:size&lt;/span&gt; &lt;span class="ss"&gt;:new_size&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"size of '1234'.size is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="s1"&gt;'1234'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the above, we’ll have an error similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; in `new_size': stack level too deep (SystemStackError)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason for this error to happen is that the alias command is executed when the monkey patch gets loaded, then when we call the &lt;code&gt;size&lt;/code&gt; method for the first time, it will execute the &lt;code&gt;new_size&lt;/code&gt; method because of the alias, the inner call to &lt;code&gt;size&lt;/code&gt; inside the &lt;code&gt;new_method&lt;/code&gt; would cause the method to call itself which ends up falling into an infinite loop. That’s why we need two aliases, as in the first example above, one for redirecting the original name to the new method and the other one for keeping access to the original implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deleting a method&lt;/strong&gt;&lt;br&gt;
Extending and overriding methods seem to be the most common way to go when doing a monkey patch, but if for any reason you end up trying to remove a method, there are some ways to do it. In this example, we’re going to remove the &lt;code&gt;odd?&lt;/code&gt; method from the core &lt;code&gt;Integer&lt;/code&gt; class using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;odd?&lt;/span&gt;
&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"undef :odd?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;odd?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this code the output should be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; true
=&amp;gt; undefined method `odd?' for 3:Integer (NoMethodError)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Considerations / Pros and cons&lt;/strong&gt;&lt;br&gt;
Being developer friendly is one of the core principles of Ruby, so there’s no coincidence that Rubyists are really into making their code as much readable as possible. With monkey patches, you have the chance to create very elegant interfaces. It doesn’t matter if it is just for the sake of improving its readability. If you think it is worth it, why don’t give it a chance?&lt;/p&gt;

&lt;p&gt;When monkey patching a core class or a third-party class, especially when extending the current implementation of a method, would require you to keep an eye on them each time you update your gems or ruby version in order to be sure that your code continues working as expected since that could potentially be a source for bugs or even a cause for breaking changes if the original implementation has changed. A good test suit and documentation would help you to deal with it.&lt;/p&gt;

&lt;p&gt;Monkey patching a core class just for having a custom method adapted to a particular need in your project is still a valid reason for doing it.&lt;/p&gt;

&lt;p&gt;If you are thinking of overriding an existing method through monkey patching, consider making use of the alias mechanism in Ruby, so you can still have access to the original implementation where it’s still needed.&lt;/p&gt;

&lt;p&gt;Having documentation is always a good practice as it helps with the maintainability of your projects, and it’s even more important when you implement this kind of technique since your might be dealing with changes in core classes, that’s why having clear and well-documented those changes is a must.&lt;/p&gt;

&lt;p&gt;As mentioned previously, the wrong implementation of a monkey patch might be the root of wired bugs even if you think you’ve applied it correctly. Future bugs are always a possibility, especially in this kind of solution, so in order to handle them in a proper way, having a good error-handling implementation would help you a lot. Including meaningful error logs would help you to faster recognize and identify bugs.&lt;/p&gt;

&lt;p&gt;If you find yourself trying to monkey patch a class of your own, better think twice about it, there are big chances that you don’t really need to go that way, that smell might indicate a needed refactor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
As always, with great power comes great responsibility 🔥. Don’t hesitate on implementing this technique if you find out that this is the best approach you can take. A takeaway I would like to emphasize is “do it responsibly by following best practices”, and always take your time to analyze the problems you are trying to solve, the more understanding you have about them, the better solutions you may end up with. Remember that is always better to think twice and code it once than the other way around.&lt;/p&gt;

&lt;p&gt;If you find yourself monkey patching a functionality, either to add some improvements that might be useful for others or to fix a bug (we all welcome bug fixes 😃), it would be great to share it with the community of the respective gem you are working with. Don’t be selfish! 😜&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Don’t let anyone tell you that a powerful technique is too scary or dangerous for you. Let it pique your curiosity instead.”&lt;/p&gt;

&lt;p&gt;DHH, Creator of Ruby on Rails. 10:06 a.m. 19 feb. 2018. &lt;a href="https://twitter.com/dhh/status/965618592606638080" rel="noopener noreferrer"&gt;Tweet&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/" rel="noopener noreferrer"&gt;https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.brice-sanchez.com/how-to-properly-monkey-patch-a-ruby-class-in-ruby-on-rails/" rel="noopener noreferrer"&gt;https://www.brice-sanchez.com/how-to-properly-monkey-patch-a-ruby-class-in-ruby-on-rails/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Monkey_patch" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Monkey_patch&lt;/a&gt;&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>offers</category>
    </item>
  </channel>
</rss>
