Skip to content

Instantly share code, notes, and snippets.

@sunny
Created September 23, 2008 11:59
Show Gist options
  • Save sunny/12268 to your computer and use it in GitHub Desktop.
Save sunny/12268 to your computer and use it in GitHub Desktop.
# Object.to_pretty
# Prints hashes and arrays on multiple lines. It reads better.
#
# Example:
# h = {"logdir"=>"/var/log/foo/", "server"=>{"sahara"=>{"osversion"=>"2.6", "osname"=>"solaris", "address"=>["10.0.0.101", "10.0.1.101"]}}, "debugfile"=>"/tmp/foo.debug"}
# puts h.to_pretty
#
# Prints:
# {
# "logdir" => "/var/log/foo/",
# "server" => {
# "sahara" => {
# "osversion" => "2.6"
# "osname" => "solaris"
# "address" => [
# "10.0.0.101",
# "10.0.1.101"
# ]
# }
# },
# "debugfile" => "/tmp/foo.debug"
# }
class Object
def to_pretty
inspect
end
end
class Array
def to_pretty
values = self.map { |v|
v.to_pretty
}.join(",\n").gsub(/^/, ' ')
"[\n#{values}\n]"
end
end
class Hash
def to_pretty
values = self.map { |k,v|
"#{k.to_pretty} => #{v.to_pretty}"
}.join(",\n").gsub(/^/, ' ')
"{\n#{values}\n}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment