title: How to assign hash values to an object while creating each key-value pair as a new object in Ruby
body: <p>I have a person object that gets a big hash of information about the person back from a webservice call (around 400 key value pairs). Each of the items in the hash is somewhat distinct, and has to be handled differently before displaying to the user (e.g. timestamp value converted, nils handled, or text otherwise transformed), etc, and the keys themselves are also important elsewhere and must be 'translated' from a fixed mapping list.</p>
  
  <p>Currently the hash of attributes is accessed directly in a Rails view, and then the return value is cleaned up in a helper, as in </p>
  
  <pre class="lang-rb prettyprint-override"><code>&lt;%= clean_up(person.attributes["some_strangly_formatted_name"]) %&gt;
  </code></pre>
  
  <p>or </p>
  
  <pre class="lang-rb prettyprint-override"><code>&lt;%= show_timestamp(person.attributes["some_nonhuman_time"]) %&gt;
  </code></pre>
  
  <p>I'm trying to figure out how to assign each of these values to the person object itself dynamically, while also creating each of the attributes as its own object of a different class (e.g. Attribute) to move related behavior there, as the amount and complexity of helper methods is getting out of control.</p>
  
  <p>Ideally in the view, I could call <code>person.some_timestamp</code> and it would return the human readable time or <code>person.name</code> and return a string of the person's name</p>
  
  <p>I've tried assigning variables as shown here, but it seems like the attribute has to be pre-defined on the object, and I'm trying to avoid 400 attr_accessors and also trying to accommodate the possibility that new attributes could be added
  <a href="https://stackoverflow.com/questions/7849521/set-attribute-dynamically-of-ruby-object">Set Attribute Dynamically of Ruby Object</a>
  <a href="https://stackoverflow.com/questions/38331671/having-a-hash-set-object-properties-in-ruby">Having a hash, set object properties in Ruby</a>
  <a href="https://stackoverflow.com/questions/3669801/dry-way-to-assign-hash-values-to-an-object">DRY way to assign hash values to an object</a></p>
  
  <p>the non-working method looks like</p>
  
  <pre class="lang-rb prettyprint-override"><code>  def process_attributes(attributes)
      attributes.each {|k,v| public_send("#{k}=", v)}
    end
  </code></pre>
  
  <p>How can I accomplish this goal? More generally, does this strategy even make sense?</p>