Skip to content

Instantly share code, notes, and snippets.

@arnemart
Last active March 22, 2023 12:08
  • Select an option

Select an option

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