title: Pass Ruby object's instance variables to another class's initializer as a map
body: <p>I'm using the <code>xml-mapping</code> gem to read some XML, and I want to feed the mapping objects into the initializers for some existing domain classes. So given XML like:</p>
  
  <pre><code>&lt;foo bar="baz"&gt;&lt;qux&gt;quux&lt;/qux&gt;&lt;/foo&gt;
  </code></pre>
  
  <p>I get an object like:</p>
  
  <pre><code>#&lt;Foo @bar="baz", @qux="quux"&gt;
  </code></pre>
  
  <p>I then want to feed it to a domain class like:</p>
  
  <pre><code>class MyFoo
    def initialize(bar:, qux:)
      # ... etc.
    end
  end
  </code></pre>
  
  <p>(Note that in <code>MyFoo</code> the attributes are read-only, and there's some validation and transformation that goes on in the initializer, so it's not simply a matter of copying instance variables from one to the other.)</p>
  
  <p>I tried transforming the instance variables to a map, thus:</p>
  
  <pre><code>foo.instance_variables.map { |name| [name, foo.instance_variable_get(name)] }.to_h
  </code></pre>
  
  <p>which produces:</p>
  
  <pre><code>{ :@bar-&gt;"baz", :@qux-&gt;"quux" }
  </code></pre>
  
  <p>This is <strong>almost</strong> what I need for the <code>MyFoo</code> initializer, but not quite -- what I need is</p>
  
  <pre><code>{ :bar-&gt;"baz", :qux-&gt;"quux" }
  </code></pre>
  
  <p>Is there a way to transform the instance variable names to symbols <strong>without</strong> the <code>@</code>-sign?</p>
  
  <p>Alternatively, is there an easier way to say "initialize yourself from all the attributes in this object"?</p>