Created
November 6, 2015 06:24
-
-
Save topher6345/6b59965858d377288a4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
string = { foo: 'bar', baz: { bill: 'bob' } }.to_json | |
def santize(string) | |
# Turn string into Hash or Array | |
result = JSON.parse( | |
string, | |
# This option lets us mutate strings | |
# By default strings are frozen | |
symbolize_names: true | |
) | |
# JSON implements our traverse function for us | |
JSON.recurse_proc( | |
result, | |
# Sanitize method goes inside this proc | |
&proc { |x| x.upcase!.reverse! if x.is_a? String } | |
) | |
result | |
end | |
puts santize(string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment