Complex map by objects property in ruby <p>I'm trying to create different kind of structure from the array I currently have in my code. </p> <p>I've got an array of objects (active directory objects) so lets say i got a from db :</p> <pre><code>a = [o1,o2,o3,o4,o5] </code></pre> <p>My object has property <code>source_id</code> and <code>name</code> which are the relevant properties.</p> <p>I want to create structure like this (I want hash) from the data I have in my array :</p> <pre><code>objects = Hash.new { |hash, key| hash[key] = [] } </code></pre> <p>And this would be one example of how to put the data inside new structure:</p> <pre><code>a.each do |ob| objects[ob.source_id] &lt;&lt; { :new =&gt; '', :name =&gt; {:unformated =&gt; ob.name, :formatted =&gt; ob.format(:name)} } end </code></pre> <p>I'm trying to replicate the same structure and it's not working out in my case :</p> <pre><code>a.group_by(&amp;:source_id).map do |k,v| { k=&gt; { { :new =&gt; '', :name =&gt; {:unformated =&gt; v.name, :formatted =&gt; ob.format(:name)} } } } end.reduce(:merge) </code></pre> <p>This is the error I get :</p> <pre><code>! #&lt;NoMethodError: undefined method `name' for #&lt;Array:0xae542b4&gt;&gt; </code></pre>