title: Iterate and Set Ruby Object Instance Variables
body: <p>I'm looking for a way to iterate through a Ruby object's instance variables, and set them individually (from a provided hash) using a generic setter.  I am assuming I can iterate through them directly in a method and simply set each individually.  </p>
  
  <p>Here is my object instance, u:</p>
  
  <pre><code>u = #&lt;Waffle:0x00007fc6b1145530 
      @id=nil,
      @alpha="",
      @bravo="",
      @charlie="",
      @delta=nil,
      @echo=nil,
      @status=new&gt;
  </code></pre>
  
  <p>I would like to populate it with a given hash, h:</p>
  
  <pre><code>h = {
      "id"=&gt;"141",
       "alpha"=&gt;"Muccahiya""
       "bravo"=&gt;"$2a$10$xR2g",
       "charlie"=&gt;"2018-02-21 10:41:56-05",
       "delta"=&gt;"2018-02-05 18:17:16.752606-05",
       "echo"=&gt;"wobbly",
       "status"=&gt;"active"
  }
  </code></pre>
  
  <p>This is the method I have and it is throwing this error:</p>
  
  <pre><code>def to_obj(h)
      self.instance_variables.each do |i|
          self.i = h[i.sub('@','')]
      end
  end
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Traceback (most recent call last):
  3: from /users/rich/app.rb:31:in `&lt;main&gt;'
  2: from /Library/WebServer/Documents/dingbat/models/waffle.rb:240:in `to_obj'
  1: from /Library/WebServer/Documents/dingbat/models/waffle.rb:240:in `each'
  /Library/WebServer/Documents/dingbat/models/waffle.rb:241:in `block in to_obj': no implicit conversion of String into Integer (TypeError)
  </code></pre>
  
  <p><code>self.instance_variables</code> is indeed an array of symbols.  </p>
  
  <p>I am thinking that an object can set its <code>instance_variables</code> given a hash.  This can't be difficult.  </p>