Skip to content

Instantly share code, notes, and snippets.

@olleolleolle
Last active December 8, 2015 05:08
Show Gist options
  • Save olleolleolle/dc96234c57535ce28e1b to your computer and use it in GitHub Desktop.
Save olleolleolle/dc96234c57535ce28e1b to your computer and use it in GitHub Desktop.
You're desperate. Your legacy code-generation code outputs a Hash using hash#to_s. You've flouted many Rubocop rules to get to where you are. Suddenly, it gets too much. You search the Web for a pretty-print function that you want: a Rubocop-friendly Hash pretty-printer.
# @param hash [Hash]
# @return [String] rubocop-friendly pretty-printed hash
def pretty_print_hash(hash)
with_19_keys = hash.to_s.gsub(/:(\w+)=>/, '\1: ')
with_whitespace = with_19_keys.sub(/^{/, '{ ').sub(/}$/,' }')
with_single_quotes = with_whitespace.gsub(/: \"([^"']*)\"/, ': \'\1\'')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment