Skip to content

Instantly share code, notes, and snippets.

@leandro
Created February 11, 2010 20:34
Show Gist options
  • Select an option

  • Save leandro/301923 to your computer and use it in GitHub Desktop.

Select an option

Save leandro/301923 to your computer and use it in GitHub Desktop.
# having a pretty dump/print of Hash objects, for debugging clarity purposes (for the sake of having a sane Hash debugging)
class Hash
def to_s(pretty = false)
return super() unless pretty
indent = lambda do |hash, tabs|
tabs += 1 if tabs == 0
indentation = "\t" * tabs
indented_output = []
for k, v in hash
key = k.is_a?(Hash) ? indent.call(k, tabs + 1) : k.inspect
value = v.is_a?(Hash) ? indent.call(v, tabs + 1) : v.inspect
indented_output << "#{key} => #{value}"
end
indented_output = [indented_output.map { |e| "#{indentation}#{e}" }.join(",\n")]
indented_output.unshift("{")
indented_output << "#{indentation[1..-1]}}"
indented_output.join("\n")
end
indent.call(self, 0)
end
end
puts({:a => 123, :b => {2 => 3, 4 => 5},{:test => :ok, :ok => :test} => [1,2,3,4]}.inspect(true))
=begin
It will print this:
{
{
:teste => :ok,
:ok => :teste
} => [1, 2, 3, 4],
:a => 123,
:b => {
2 => 3,
4 => 5
}
}
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment