title: Update Ruby class attributes hash when a property changes
body: <p>I'm trying to write a Ruby class that works similarly to Rails AactiveRecord model in the way that attributes are handled:</p>
  
  <pre><code>class Person
    attr_accessor :name, :age
  
    # init with Person.new(:name =&gt; 'John', :age =&gt; 30)
    def initialize(attributes={})
      attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") }
      @attributes = attributes
    end
  
    # read attributes
    def attributes
      @attributes
    end
  
    # update attributes
    def attributes=(attributes)
      attributes.each do |key, val| 
        if respond_to?("#{key}=")
          send("#{key}=", val) 
          @attributes[key] = name
        end
      end
    end
  end
  </code></pre>
  
  <p>What I mean is that when I init the class, an "attributes" hash is updated with the relevant attributes:</p>
  
  <pre><code>&gt;&gt;&gt; p = Person.new(:name =&gt; 'John', :age =&gt; 30)
  &gt;&gt;&gt; p.attributes
   =&gt; {:age=&gt;30, :name=&gt;"John"}
  &gt;&gt;&gt; p.attributes = { :name =&gt; 'charles' }
  &gt;&gt;&gt; p.attributes
   =&gt; {:age=&gt;30, :name=&gt;"charles"}
  </code></pre>
  
  <p>So far so good. What I want to happen is for the attributes hash to update when I set an individual property:</p>
  
  <pre><code>&gt;&gt;&gt; p.attributes
   =&gt; {:age=&gt;30, :name=&gt;"John"}
  &gt;&gt;&gt; p.name
   =&gt; "John"
  &gt;&gt;&gt; p.name = 'charles' # &lt;--- update an individual property
   =&gt; "charles"
  &gt;&gt;&gt; p.attributes
   =&gt; {:age=&gt;30, :name=&gt;"John"} # &lt;--- should be {:age=&gt;30, :name=&gt;"charles"}
  </code></pre>
  
  <p>I could do that by writing a setter and getter for every attribute instead of using <code>attr_accessor</code>, but that'll suck for a model that has a lot of fields. Any quick way to accomplish this?</p>