title: Ruby to_json on object to output instance variables
body: <p>I'm using Sinatra and trying to output an object in JSON by using the 'json' gem and calling the .to_json method. I expected the output to be JSON with the symbols in the attr_reader section and their values.</p>
  
  <p>Here's my code. Do I need to do anything special to get this to work?</p>
  
  <pre><code>require "sinatra"
  require "json"    
  
  class Foo
    attr_reader :id, :name
  
    def initialize(id, name)
      @id = id
      @name = name
    end
  end
  
  get '/start' do
    content_type :json
    Foo.new(2, "john").to_json
  end
  </code></pre>
  
  <p>All I get from the output is the objects default to_s.</p>
  
  <pre><code>"#&lt;Foo:0x007fe372a3ba80&gt;"
  </code></pre>