Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Last active December 2, 2019 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/bb3e95a08b9e0d8e48c3f62ef8097464 to your computer and use it in GitHub Desktop.
Save sixtyfive/bb3e95a08b9e0d8e48c3f62ef8097464 to your computer and use it in GitHub Desktop.
def array_wrap(obj)
case obj
when Array then obj.map(&method(:array_wrap_hash_values))
when Hash then array_wrap([obj])
else obj
end
end
def array_wrap_hash_values(obj)
case obj
when Hash then obj.transform_values(&method(:array_wrap))
else obj
end
end
maybewithout = {eins: 1, zwei: 2, drei: {vier: 3, fünf: 4}}
with = array_wrap(maybewithout)
pp with # should show "[eins: 1, zwei: 2, drei: [{vier: 3, fünf: 4}]]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment