<?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: Aaron Roh</title>
    <description>The latest articles on DEV Community by Aaron Roh (@roharon).</description>
    <link>https://dev.to/roharon</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%2F1468411%2Facf0a0df-33bd-4043-9d05-67c2ff408b55.jpeg</url>
      <title>DEV Community: Aaron Roh</title>
      <link>https://dev.to/roharon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roharon"/>
    <language>en</language>
    <item>
      <title>Rails Core Classes Method Lookup Changes: A Deep Dive into Include vs Prepend</title>
      <dc:creator>Aaron Roh</dc:creator>
      <pubDate>Tue, 07 May 2024 09:04:54 +0000</pubDate>
      <link>https://dev.to/roharon/rails-core-classes-method-lookup-changes-a-deep-dive-into-include-vs-prepend-3c26</link>
      <guid>https://dev.to/roharon/rails-core-classes-method-lookup-changes-a-deep-dive-into-include-vs-prepend-3c26</guid>
      <description>&lt;h3&gt;
  
  
  What Happened?
&lt;/h3&gt;

&lt;p&gt;on April 23, 2024, a PR &lt;a href="https://github.com/rails/rails/pull/51640"&gt;#51640&lt;/a&gt; was merged into main branch of Ruby On Rails.&lt;br&gt;
This PR title is &lt;code&gt;Use Module#include rather than prepend for faster method lookup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Previously, if you printed &lt;code&gt;Integer.ancestors&lt;/code&gt; in Rails, you would see 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="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;NumericWithFormat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
 &lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Tryable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the PR was merged, &lt;code&gt;Integer.ancestors&lt;/code&gt; changed to:&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="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;NumericWithFormat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Tryable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The priority of the &lt;code&gt;ActiveSupport::NumericWithFormat&lt;/code&gt; module for &lt;code&gt;Integer&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, and &lt;code&gt;BigDecimal&lt;/code&gt; changed from &lt;code&gt;prepend&lt;/code&gt; to &lt;code&gt;include&lt;/code&gt;.&lt;br&gt;
Additionally, the priority of the &lt;code&gt;ActiveSupport::ToJsonWithActiveSupportEncoder&lt;/code&gt; module changed from include to prepend. As a result, the order of ancestors for &lt;code&gt;[Enumerable, Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass]&lt;/code&gt; changed.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is changed?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;In summary, the order of the ancestors for core classes, where modules are defined in Rails, has been altered.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The order of &lt;code&gt;ActiveSupport::NumericWithFormat&lt;/code&gt; and &lt;code&gt;ActiveSupport::ToJsonWithActiveSupportEncoder&lt;/code&gt; has been moved to behind the core class.&lt;/p&gt;

&lt;p&gt;Before this PR was merged, the method lookup order for Integer was as follows.&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="c1"&gt;# Integer.ancestors&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;NumericWithFormat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Tryable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the order of ancestors for Integer is as follows.&lt;br&gt;
You can see that &lt;code&gt;ActiveSupport::NumericWithFormat&lt;/code&gt; and &lt;code&gt;ActiveSupport::ToJsonWithActiveSupportEncoder&lt;/code&gt; order are moved to after Integer.&lt;br&gt;
Additionally, changes were made to the Integer, Float, BigDecimal, Enumerable, Object, NilClass and other core class.&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="c1"&gt;# Integer.ancestors&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;NumericWithFormat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;PP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ObjectMixin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ToJsonWithActiveSupportEncoder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Ext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Generator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GeneratorMethods&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Tryable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;Kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;BasicObject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Rails, some core classes have mixed-in modules like &lt;code&gt;ActiveSupport::NumericWithFormat&lt;/code&gt;, &lt;code&gt;ActiveSupport::ToJsonWithActiveSupportEncoder&lt;/code&gt;.&lt;br&gt;
These modules implement methods that are useful for the class, such as &lt;code&gt;12345678.to_fs(:delimited)&lt;/code&gt;, which is not available in pure Ruby.&lt;br&gt;
These implemented methods do not override pure Ruby's methods; they simply add new methods to the class. Therefore, there is no reason to use prepend for these mixed-in modules.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why is that problem?
&lt;/h3&gt;

&lt;p&gt;In TruffleRuby, certain interpreter optimizations are performed to enhance the execution speed of the code.&lt;br&gt;
However, when &lt;code&gt;prepend&lt;/code&gt; is used on core classes such as &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Integer&lt;/code&gt;, these optimizations are undone.&lt;br&gt;
This is because prepend alters the method lookup order, which can lead to more complex method resolution and hamper the performance benefits provided by VM optimization that include Method inlining and Method caching.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is Ruby Method Lookup?
&lt;/h3&gt;

&lt;p&gt;Ruby does not have multiple inheritance like C++ or Java.&lt;br&gt;
But Ruby supports using mix-in modules and extend singleton classes at runtime.&lt;br&gt;
So when you call a method on an object, Ruby will look for the method in the object's class, then in the included modules, and finally in the superclass.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;Method Lookup&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I have prepared an example for easy explanation.&lt;/p&gt;

&lt;p&gt;There are classes, &lt;code&gt;String1&lt;/code&gt; and &lt;code&gt;String2&lt;/code&gt; that inherit from &lt;code&gt;String&lt;/code&gt; class.&lt;br&gt;
And &lt;code&gt;CustomModule&lt;/code&gt; module is included in &lt;code&gt;String1&lt;/code&gt; and prepended in &lt;code&gt;String2&lt;/code&gt;.&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;module&lt;/span&gt; &lt;span class="nn"&gt;CustomModule&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="s1"&gt;'defined on CustomModule'&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;class&lt;/span&gt; &lt;span class="nc"&gt;String1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;CustomModule&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="s1"&gt;'defined on String1'&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;class&lt;/span&gt; &lt;span class="nc"&gt;String2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="no"&gt;CustomModule&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="s1"&gt;'defined on String2'&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;When we call &lt;code&gt;foo&lt;/code&gt; method on &lt;code&gt;String1&lt;/code&gt; and &lt;code&gt;String2&lt;/code&gt;, the result is like below.&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;p&lt;/span&gt; &lt;span class="no"&gt;String1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'foobar'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; defined on String1&lt;/span&gt;

&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="no"&gt;String2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'foobar'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; defined on CustomModule&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result of &lt;code&gt;String1.new('foobar').foo&lt;/code&gt; and &lt;code&gt;String2.new('foobar').foo&lt;/code&gt; is different.&lt;br&gt;
Because, &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;prepend&lt;/code&gt; have different method lookup path.&lt;br&gt;
&lt;code&gt;include&lt;/code&gt; will add the module after the methods of the class itself, while &lt;code&gt;prepend&lt;/code&gt; will add the module to the beginning of the method lookup path.&lt;/p&gt;

&lt;p&gt;This means, when you defined prepend module and call a method on an object, Ruby will first look for the method in the prepended module. So prepend module will be called.&lt;br&gt;
And when you defined include module and call a method on an object, Ruby will first look for the method in the object's class. So the class method will be called.&lt;/p&gt;

&lt;p&gt;Method lookup path for &lt;code&gt;String1&lt;/code&gt; and &lt;code&gt;String2&lt;/code&gt; is like below.&lt;br&gt;
It sounds reasonable when we call &lt;code&gt;foo&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String1&lt;/code&gt; Class Method Lookup Path&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn5qr7mteyg0sx6nvheu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn5qr7mteyg0sx6nvheu.png" alt="String1 Class Method Lookup Path - using String1, String1 class didn't found foo method, so step to next sequence. this sequence is CustomModule. and found method here. " width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String2&lt;/code&gt; Class Method Lookup Path&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnic5qndxza27xmv7zrsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnic5qndxza27xmv7zrsk.png" alt="String2 Class Method Lookup Path" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, calling other common methods will be much more frequent than calling method foo on String1 and String2 class.&lt;/p&gt;

&lt;p&gt;So, without special reason, it's better to use &lt;code&gt;include&lt;/code&gt; rather than &lt;code&gt;prepend&lt;/code&gt; for mixed-in modules in core classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Rails have changed the method lookup path for core classes.&lt;br&gt;
This change is to improve the performance of the code execution in TruffleRuby (relates with method caching).&lt;/p&gt;

&lt;p&gt;However, in MRI, I think this change will have little to no impact on the performance of code execution.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I Referenced below list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rails/rails/pull/51640"&gt;https://github.com/rails/rails/pull/51640&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/oracle/truffleruby/issues/3546"&gt;https://github.com/oracle/truffleruby/issues/3546&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

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