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:
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:
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:
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:
I shouldn't need to initialize an empty array -- surely
.each_with_object
?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 aselect
instead? (Just an example, not sure that select does what I want, although it could also take care of creating the containing array...)Again, initializing the empty hash seems wrong --
.each_with_object
again?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:
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:
[
{
: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.
structure_hash
up front. Iterate the flattened hashes array to transform it. Nested.each
becomes sequential. If anything is a Ruby idiom it's "everything is an object." and "objects work like you expect them to". That means plenty of helpful methods. - hmmm,... Consider a Class that can return its own flattened representation. Then, I suppose aVhost.flatten.transform
call could have a default-value code block parameter. \$\endgroup\$def flatten_hash(hash, subkey); hash.map { |k,v| v.dup.tap { |h| h[subkey] = k} }
\$\endgroup\$