Skip to main content
Tweeted twitter.com/StackCodeReview/status/1411701336291397635
deleted 36 characters in body
Source Link
Steve K
  • 135
  • 5

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, I don't it gets to the right idiomatic solution becauseit's not idiomatic; it just replaces nested .each with the slightly more helpful each_with_object.

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, I don't it gets to the right idiomatic solution because it just replaces nested .each with the slightly more helpful each_with_object.

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.

Edited title as requested
Link
Steve K
  • 135
  • 5

How can I think more idiomatically about Reshape hash of hashes into array of modified hashes without using nested data structures in Rubyeach?

edited body
Source Link
Steve K
  • 135
  • 5
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
    demo_structurestructure_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
def get_vhost_data_refactor
    demo_structurestructure_hash = {...}
    demo_structurestructure_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
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
    demo_structure.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
def get_vhost_data_refactor
    demo_structure = {...}
    demo_structure.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
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
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
Source Link
Steve K
  • 135
  • 5
Loading