Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am unsure of what you are trying to do from your example. As a general rule, in Ruby, you don't check for types. It is dynamically typed for a reason: you can write code that works for any objects that happen to support the methods your code calls on them.</p> <p>From what I can tell from your comments, you want to extend the class <code>Array</code> so, when you call a method on an array like <code>an_array.pack</code>, the array is searched for an instance of <code>Pack</code> and returned. Ruby has a method called whenever a method is found not to exist called <code>Module#method_missing</code>. For example, if I randomly decide to call <code>4.to_dragon(magic: 4, height: 700)</code>, the Ruby interpreter will attempt to find <code>to_dragon</code> as a public method defined on some class or module in the <code>Fixnum</code> (type of numbers) inheritance chain. Provided you have not done something strange to that chain, we get a call to <code>method_missing</code> on the object <code>4</code> with these arguments: <code>[:to_dragon, { magic: 4, height: 700 }]</code>. Basically, that's the name appended to the front of the arguments, and a block should one be given.</p> <p>Using this technique, you can override <code>method_missing</code> to get this code as a solution:</p> <pre><code>class String def method_to_class split('_').map(&amp;:capitalize).join end end class Array def method_missing(method_name, *arguments, &amp;block) find do |element| element.class.name == method_name.to_s.method_to_class end || super end end </code></pre> <p>You add a method to <code>String</code> to convert a method name to a class name. Then, you redefine <code>method_missing</code> on <code>Array</code> to check each element to see if the class name matches the given class name. If one is found, then that is returned. Otherwise (and we do this using Ruby's fancy <code>||</code> operator), the value returned from that function is <code>nil</code> and the second operand to <code>||</code> is returned. This happens to be the default implementation for <code>method_missing</code> (which we get by the <code>super</code> keyword) and returns the error the call deserves.</p> <p>The only potential issue with that is, if you have elements that have class names identical to method names that are already defined on arrays, then those will be called instead rather than this special technique. For example, calling <code>an_array.hash</code> will give you the hash code of the array rather than the first instance of a hash.</p> <p>A safer technique in this respect is more similar to what I think you were trying to do. It actually uses class objects, and you can use it to override other methods:</p> <pre><code>class Array def add(class_object) class &lt;&lt; self define_method class_object.name do find do |element| element.is_a? class_object end end end end end </code></pre> <p>This defines new methods directly on an instance of an array. For example:</p> <pre><code>an_array = [Array, Hash, Dragon].map &amp;:new an_array.add Hash an_array.hash #=&gt; {} </code></pre> <p>If this answer does not include a solution, it should at least guide you closer!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload