<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hashrocket on Medium]]></title>
        <description><![CDATA[Stories by Hashrocket on Medium]]></description>
        <link>https://medium.com/@hashrocket?source=rss-a089b63fd8e5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bT_hdvz-3j7jsKsr.png</url>
            <title>Stories by Hashrocket on Medium</title>
            <link>https://medium.com/@hashrocket?source=rss-a089b63fd8e5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 08:54:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hashrocket/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Ruby’s Method Class for Fun and Profit]]></title>
            <link>https://medium.com/hashrocket-s-collections/using-ruby-s-method-class-for-fun-and-profit-95b382a2725?source=rss-a089b63fd8e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/95b382a2725</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Hashrocket]]></dc:creator>
            <pubDate>Fri, 05 Feb 2016 07:10:41 GMT</pubDate>
            <atom:updated>2016-02-05T07:10:41.669Z</atom:updated>
            <content:encoded><![CDATA[<p>Lets try to use some of the methods in Class: Method</p><p>First some setup, we will create a Foo module with a bar method and include it in the String class.</p><pre><strong>module</strong> Foo<br>  <strong>def</strong> bar(x)<br>    self &lt;&lt; &quot;#{x}&quot;<br>  <strong>end</strong><br><strong>end</strong><br>String.send(:include, Foo)</pre><p>We will start with a String instance.</p><pre>pry(main)&gt; str = &quot;foo&quot;<br>=&gt; &quot;foo&quot;</pre><p>We can create an instance of the Method class like so:</p><pre>pry(main)&gt; bar_meth = str.method(:bar)<br>=&gt; <em>#&lt;Method: String(Foo)#bar&gt;</em></pre><p>If we wanted to we could invoke this with Method#call, but we are more interested in the Method instance.</p><pre>pry(main)&gt; bar_meth.call(&quot;baz&quot;)<br>=&gt; &quot;foobaz&quot;</pre><p>We often want to know where a method is defined. We can use Method#source_location to find out. I loaded the initial Foo module into the REPL in a file called ‘wut.rb’</p><pre>pry(main)&gt; bar_meth.source_location<br>=&gt; [&quot;wut.rb&quot;, 2]</pre><p>If the method is defined in the core library Method#source_location will return nil.</p><pre>pry(main)&gt; str.method(:length).source_location<br>=&gt; nil</pre><p>We could wrap this in a predicate method called core? and add to Method</p><pre><strong>module</strong> MethodHelpers<br>  <strong>def</strong> core?<br>    self.source_location.nil?<br>  <strong>end</strong><br><strong>end</strong><br>Method.send(:include, MethodHelpers)</pre><pre>pry(main)&gt; bar_meth.core?<br>=&gt; false</pre><p>We can check where the method of interest is scoped with Method#owner</p><pre>pry(main)&gt; bar_meth.owner<br>=&gt; Foo</pre><p>This means we can check the instance methods en masse for any modules or external libraries in our inheritance chain. Here I will create a Hash with the owners as keys and the values an array of their methods.</p><pre>pry(main)&gt; str.methods.each_with_object(Hash.new {|h, k| h[k] = []}) <strong>do</strong> |m, h| <br>  h[str.method(m).owner] &lt;&lt; m<br><strong>end</strong><br>=&gt; {String=&gt;<br>  [:&lt;=&gt;,<br>   :==,<br>   :===,<br>   :eql?,<br>   :hash,<br>   :casecmp,<br>   ...<br>   Foo=&gt;[:bar]<br>   ...<br>  ]}</pre><p>We can add a little more information to this if we throw in the Method#arity and Method#parameters methods in there as well.</p><pre>pry(main)&gt; str.methods.each_with_object(Hash.new {|h, k| h[k] = []}) <strong>do</strong> |m, h| <br>  h[str.method(m).owner] &lt;&lt; [m, str.method(m).arity, str.method(m).parameters] <br><strong>end</strong></pre><pre>=&gt; {String=&gt;<br>  [[:&lt;=&gt;, 1, [[:req]]],<br>   [:==, 1, [[:req]]],<br>   [:===, 1, [[:req]]],<br>   [:eql?, 1, [[:req]]],<br>   [:hash, 0, []],<br>   [:casecmp, 1, [[:req]]],<br>   ...<br>   Foo=&gt;[[:bar, 1, [[:req, :x]]]],<br>   ...<br>  ]}</pre><p>Now let’s create a new Object#methods. We will call this #method_list and I’ll put it on object as well. It will look similar to the previous hash. I’m going to addMethod#source_location to the values array and a parameter for passing a boolean if you want to exclude core methods from the list. We can also significantly clean it up by iterating over an array of Method objects instead of the method name.</p><pre><strong>module</strong> MethodList<br>  <strong>def</strong> method_list(core = true)<br>    total_meth    = self.methods.map {|m| self.method(m) }<br>    non_core_meth = self.methods.map {|m| self.method(m) }.select { |m| !m.core? }</pre><pre>    list = core ? total_meth : non_core_meth</pre><pre>    list.each_with_object(Hash.new {|h, k| h[k] = []}) <strong>do</strong> |m, h|<br>      h[m.owner] &lt;&lt; [m.name, m.arity, m.parameters, m.source_location]<br>    <strong>end</strong><br>  <strong>end</strong><br><strong>end</strong><br>Object.send(:include, MethodList)</pre><pre>pry(main)&gt; str.method_list(false)<br>=&gt;...<br>Foo=&gt;[[:bar, 1, [[:req, :x]], [&quot;wut.rb&quot;, 2]]],<br>...</pre><p>And there we have it — usefulness debatable. I find Method#source_location invaluable for looking up the source in gems, but I would be curious to hear how other people are using any of the other methods in the Method class.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95b382a2725" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hashrocket-s-collections/using-ruby-s-method-class-for-fun-and-profit-95b382a2725">Using Ruby’s Method Class for Fun and Profit</a> was originally published in <a href="https://medium.com/hashrocket-s-collections">Hashrocket’s Collections</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>