pp
pp copied to clipboard
Ruby 1.9 hash symbol syntax
New hash syntax was introduced in ruby 1.9 around 2009 (14 years ago?)
this patch will use new syntax when possible and add spaces around =>
What do you think about making this as default behavior?
module PP::PPMethods
def pp_hash(obj)
group(1, '{', '}') {
seplist(obj, nil, :each_pair) {|key, v|
group {
if key.is_a?(Symbol)
if key.to_s =~ /^[\w_][\w\d_]+$/i
text "#{key}: "
else
text "'#{key}': "
end
else
pp key
text ' => '
end
group(1) {
breakable ''
pp v
}
}
}
}
end
end
# example:
pp(
"string key" => 1111,
"33 symbol .": 222,
symbol: 333,
sub: {
hash: {
_aaa_aaaa_111: 444,
something_else: "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml",
"and" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml"
}
}
)
# prints
{"string key" => 1111,
'33 symbol .': 222,
symbol: 333,
sub:
{hash:
{_aaa_aaaa_111: 444,
something_else: "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml",
"and" => "eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJhdWQiOlsiZ29qZWsiLCJtaWR0cmFucyIsImdvdml"}}}