Last active
March 22, 2023 12:08
-
-
Save arnemart/3935ff4863cb57ec94fa to your computer and use it in GitHub Desktop.
Pretty print json from irb
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this! Such a great idea. I think line 6 needs to be
if json.class == String