title: Why do this Ruby object have both to_s and inspect methods that appear to do the same thing?
body: <p>Why do this Ruby object both a <code>to_s</code> and <code>inspect</code> methods that appear to do the same thing?</p>
  
  <p>The <code>p</code> method calls <code>inspect</code> and puts/print calls <code>to_s</code> for representing the object.</p>
  
  <p>If I run</p>
  
  <pre><code>class Graph
    def initialize
      @nodeArray = Array.new
      @wireArray = Array.new
    end
    def to_s # called with print / puts
      "Graph : #{@nodeArray.size}"
    end
    def inspect # called with p
      "G"
    end
  end
  
  if __FILE__ == $0
    gr = Graph.new
    p gr
    print gr
    puts gr
  end
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>G
  Graph : 0
  Graph : 0
  </code></pre>
  
  <ul>
  <li>Then, why does Ruby have two functions do the same thing? What is the difference between <code>to_s</code> and <code>inspect</code>?</li>
  <li>And what's the difference between <code>puts</code>, <code>print</code>, and <code>p</code>?</li>
  </ul>
  
  <p>If I comment out the <code>to_s</code> or <code>inspect</code> function, I get as follows.</p>
  
  <pre><code>#&lt;Graph:0x100124b88&gt;
  #&lt;Graph:0x100124b88&gt;
  </code></pre>