title: In Ruby, how does coerce() actually work?
body: <p>It is said that when we have a class <code>Point</code> and knows how to perform <code>point * 3</code> like the following:</p>
  
  <pre><code>class Point
    def initialize(x,y)
      @x, @y = x, y
    end
  
    def *(c)
      Point.new(@x * c, @y * c)
    end
  end
  
  point = Point.new(1,2)
  p point
  p point * 3
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>#&lt;Point:0x336094 @x=1, @y=2&gt;
  #&lt;Point:0x335fa4 @x=3, @y=6&gt;
  </code></pre>
  
  <p>but then, </p>
  
  <pre><code>3 * point
  </code></pre>
  
  <p>is not understood:</p>
  
  <blockquote>
    <p><code>Point</code> can't be coerced into <code>Fixnum</code> (<code>TypeError</code>)</p>
  </blockquote>
  
  <p>So we need to further define an instance method <code>coerce</code>:</p>
  
  <pre><code>class Point
    def coerce(something)
      [self, something]
    end
  end
  
  p 3 * point
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>#&lt;Point:0x3c45a88 @x=3, @y=6&gt;
  </code></pre>
  
  <p>So it is said that <code>3 * point</code> is the same as <code>3.*(point)</code>. That is, the instance method <code>*</code> takes an argument <code>point</code> and invoke on the object <code>3</code>.</p>
  
  <p>Now, since this method <code>*</code> doesn't know how to multiply a point, so</p>
  
  <pre><code>point.coerce(3)
  </code></pre>
  
  <p>will be called, and get back an array:</p>
  
  <pre><code>[point, 3]
  </code></pre>
  
  <p>and then <code>*</code> is once again applied to it, is that true?</p>
  
  <p>Now, this is understood and we now have a new <code>Point</code> object, as performed by the instance method <code>*</code> of the <code>Point</code> class.</p>
  
  <p>The question is:</p>
  
  <ol>
  <li><p>Who invokes <code>point.coerce(3)</code>? Is it Ruby automatically, or is it some code inside of <code>*</code> method of <code>Fixnum</code> by catching an exception?  Or is it by <code>case</code> statement that when it doesn't know one of the known types, then call <code>coerce</code>?</p></li>
  <li><p>Does <code>coerce</code> always need to return an array of 2 elements?  Can it be no array?  Or can it be an array of 3 elements?</p></li>
  <li><p>And is the rule that, the original operator (or method) <code>*</code> will then be invoked on element 0, with the argument of element 1?  (Element 0 and element 1 are the two elements in that array returned by <code>coerce</code>.)   Who does it?  Is it done by Ruby or is it done by code in <code>Fixnum</code>?  If it is done by code in <code>Fixnum</code>, then it is a "convention" that everybody follows when doing a coercion?</p>
  
  <p>So could it be the code in <code>*</code> of <code>Fixnum</code> doing something like this:</p>
  
  <pre><code>class Fixnum
    def *(something)
      if (something.is_a? ...)
      else if ...  # other type / class
      else if ...  # other type / class
      else
      # it is not a type / class I know
        array = something.coerce(self)
        return array[0].*(array[1])   # or just return array[0] * array[1]
      end
    end
  end
  </code></pre></li>
  <li><p>So it is really hard to add something to <code>Fixnum</code>'s instance method <code>coerce</code>?  It already has a lot of code in it and we can't just add a few lines to enhance it (but will we ever want to?)</p></li>
  <li><p>The <code>coerce</code> in the <code>Point</code> class is quite generic and it works with <code>*</code> or <code>+</code> because they are transitive.  What if it is not transitive, such as if we define Point minus Fixnum to be:</p>
  
  <pre><code>point = Point.new(100,100)
  point - 20  #=&gt; (80,80)
  20 - point  #=&gt; (-80,-80)
  </code></pre></li>
  </ol>