My goal is to write more idiomatic ruby when working with nested data structures. More specifically, I need to break my habit of looping through everything with `.each` and nested `.each`es. Here's a simple example that illustrates this: I have the following data structure which cannot be changed: ```ruby structure_hash = { "website"=> { "base"=>"luma.demo", "custom_b2c_website"=>"homegoods.demo", }, "store_view"=> { "custom_store_view"=>"fr.homegoods.ca" } } ``` I want to reshape this such that I return the data as an array of hashes like so: ```ruby structure_array = [ { :scope=>"website", :code=>"base", :url=>"luma.demo" }, { :scope=>"website", :code=>"custom_b2c_website", :url=>"homegoods.demo" }, { :scope=>"store", :code=>"custom_store_view", :url=>"fr.homegoods.ca" } ] ``` I have achieved this with the following helper method with comments that illustrate the thought process I am trying to improve: ```ruby def get_vhost_data structure_hash = { ... } # The first hash above, etc. vhost_data = [] # We're returning an array, so initialize one # Loop through the containing hash. Each key will be our scope, so we need to # access that structure_hash.each do |scope, scope_hash| # We need to transform and add to the data in these hashes. scope_hash.each do |code, url| # Best to return a new hash to house the old values + the transformed ones demo_data = {} # The "scope" key from the outer hash needs to be changed conditionally demo_data[:scope] = scope == 'store_view' ? scope.gsub('store_view', 'store') : scope # The rest of the data is fine demo_data[:code] = code demo_data[:url] = url # Add the newly-created hash to the containing array vhost_data << demo_data end end # Return the containing array vhost_data end ``` What Smells? ------------ As best I can tell, the following things are fishy: 1. I shouldn't need to initialize an empty array -- surely `.each_with_object`? 2. Nested `.each` here seems tedious -- is there a better way to think about what I'm trying to do that would result in something more idiomatic? For example, instead of resorting to "Okay, we need to go through each hash and..." is it more idiomatic to say: "Since you're only manipulating one of the keys of the outer hash, use a `select` instead? (Just an example, not sure that select does what I want, although it could also take care of creating the containing array...) 3. Again, initializing the empty hash seems wrong -- `.each_with_object` again? 4. Looping through a hash to create a new hash from the existing hash's content and add to it. At first I thought `map` would be better somehow, but in my limited understanding, `.map` takes existing elements and transforms them -- it doesn't add additional elements... What I've Tried ------------- So far, I've tried the following to address the code smells above: ```ruby def get_vhost_data_refactor structure_hash = {...} structure_hash.each_with_object([]) do |(scope, scope_hash), vhost_arr| scope_hash.each_with_object({}) do |(code, url), data_hash| data_hash[:scope] = scope == 'store_view' ? scope.gsub('store_view', 'store') : scope data_hash[:code] = code data_hash[:url] = url vhost_arr << data_hash end end end ``` which yields: ```ruby [ { :scope=>"website", :code=>"custom_b2c_website_3", :url=>"sierra.demo" }, { :scope=>"website", :code=>"custom_b2c_website_3", :url=>"sierra.demo" }, { :scope=>"store", :code=>"custom_store_view", :url=>"fr.homegoods.ca" } ] ``` This is close, but obviously, the `.each_with_object` combination doesn't loop through each of the inner hashes properly, and more importantly, even if it *did* work, it's not idiomatic; it just replaces nested `.each` with the slightly more helpful `each_with_object`. Any advice on how I can solve this "the Ruby way" and any tips for questions to ask in order to *think* "the Ruby way" would be greatly appreciated.