[ruby/pp] Extract pp_hash_pair
The method which prints single pair of a hash, to make extending pretty printing Hash easier, apart from Hash construct itself. https://github.com/ruby/pp/commit/3fcf2d1142
This commit is contained in:
parent
37b8fc7477
commit
6e704311bb
17
lib/pp.rb
17
lib/pp.rb
@ -286,16 +286,21 @@ class PP < PrettyPrint
|
|||||||
group(1, '{', '}') {
|
group(1, '{', '}') {
|
||||||
seplist(obj, nil, :each_pair) {|k, v|
|
seplist(obj, nil, :each_pair) {|k, v|
|
||||||
group {
|
group {
|
||||||
pp k
|
pp_hash_pair k, v
|
||||||
text '=>'
|
|
||||||
group(1) {
|
|
||||||
breakable ''
|
|
||||||
pp v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A pretty print for a pair of Hash
|
||||||
|
def pp_hash_pair(k, v)
|
||||||
|
pp k
|
||||||
|
text '=>'
|
||||||
|
group(1) {
|
||||||
|
breakable ''
|
||||||
|
pp v
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
include PPMethods
|
include PPMethods
|
||||||
|
@ -247,25 +247,18 @@ end
|
|||||||
|
|
||||||
class PPInheritedTest < Test::Unit::TestCase
|
class PPInheritedTest < Test::Unit::TestCase
|
||||||
class PPSymbolHash < PP
|
class PPSymbolHash < PP
|
||||||
def pp_hash(obj)
|
def pp_hash_pair(k, v)
|
||||||
group(1, "{", "}") {
|
case k
|
||||||
seplist(obj, nil, :each_pair) {|k, v|
|
when Symbol
|
||||||
case k
|
text k.inspect.delete_prefix(":")
|
||||||
when Symbol
|
text ":"
|
||||||
text k.inspect.delete_prefix(":")
|
group(1) {
|
||||||
text ":"
|
breakable
|
||||||
sep = " "
|
pp v
|
||||||
else
|
|
||||||
pp k
|
|
||||||
text "=>"
|
|
||||||
sep = ""
|
|
||||||
end
|
|
||||||
group(1) {
|
|
||||||
breakable sep
|
|
||||||
pp v
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user