Skip to content

Instantly share code, notes, and snippets.

@topher6345
Created November 6, 2015 06:24
Show Gist options
  • Save topher6345/6b59965858d377288a4f to your computer and use it in GitHub Desktop.
Save topher6345/6b59965858d377288a4f to your computer and use it in GitHub Desktop.
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