Skip to content

Instantly share code, notes, and snippets.

@arnemart
Last active March 22, 2023 12:08
Show Gist options
  • Save arnemart/3935ff4863cb57ec94fa to your computer and use it in GitHub Desktop.
Save arnemart/3935ff4863cb57ec94fa to your computer and use it in GitHub Desktop.
Pretty print json from irb
def ppjson(json)
puts _ppjson(json)
end
def _ppjson(json)
json = JSON.parse(json) if json.class == "String"
JSON.pretty_generate(json)
end
class String
require 'json'
def ppjson
puts _ppjson(self)
end
end
class Hash
def ppjson
puts _ppjson self
end
end
@jvon1904
Copy link

Thanks for sharing this! Such a great idea. I think line 6 needs to be if json.class == String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment