mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* (lib/pp.rb, lib/prettyprint.rb): define seplist in PP::PPMethods
instead of PrettyPrint. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6032 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3952564936
commit
4d664a098d
3 changed files with 51 additions and 39 deletions
|
@ -1,3 +1,8 @@
|
|||
Sat Mar 27 10:40:48 2004 Tanaka Akira <akr@m17n.org>
|
||||
|
||||
* (lib/pp.rb, lib/prettyprint.rb): define seplist in PP::PPMethods
|
||||
instead of PrettyPrint.
|
||||
|
||||
Sat Mar 27 01:47:09 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
|
||||
|
||||
* lib/logger.rb: trim tail space of each line. no user visible change.
|
||||
|
|
46
lib/pp.rb
46
lib/pp.rb
|
@ -88,6 +88,32 @@ PP#pp to print the object.
|
|||
text ','
|
||||
breakable
|
||||
|
||||
--- seplist(list[, separator_proc[, iter_method]]) {|elt| ... }
|
||||
adds a separated list.
|
||||
The list is separated by comma with breakable space, by default.
|
||||
|
||||
seplist iterates the ((|list|)) using ((|iter_method|)).
|
||||
It yields each object to the block given for seplist.
|
||||
The procedure ((|separator_proc|)) is called between each yields.
|
||||
|
||||
If the iteration is zero times, ((|separator_proc|)) is not called at all.
|
||||
|
||||
If ((|separator_proc|)) is nil or not given,
|
||||
(({lambda { comma_breakable }})) is used.
|
||||
If ((|iter_method|)) is not given, (({:each})) is used.
|
||||
|
||||
For example, following 3 code fragments has similar effect.
|
||||
|
||||
q.seplist([1,2,3]) {|v| xxx v }
|
||||
|
||||
q.seplist([1,2,3], lambda { comma_breakable }, :each) {|v| xxx v }
|
||||
|
||||
xxx 1
|
||||
q.comma_breakable
|
||||
xxx 2
|
||||
q.comma_breakable
|
||||
xxx 3
|
||||
|
||||
= Object
|
||||
--- pretty_print(pp)
|
||||
is a default pretty printing method for general objects.
|
||||
|
@ -201,6 +227,19 @@ class PP < PrettyPrint
|
|||
breakable
|
||||
end
|
||||
|
||||
def seplist(list, sep=nil, iter_method=:each)
|
||||
sep ||= lambda { comma_breakable }
|
||||
first = true
|
||||
list.__send__(iter_method) {|*v|
|
||||
if first
|
||||
first = false
|
||||
else
|
||||
sep.call
|
||||
end
|
||||
yield(*v)
|
||||
}
|
||||
end
|
||||
|
||||
def pp_object(obj)
|
||||
object_address_group(obj) {
|
||||
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
||||
|
@ -603,4 +642,11 @@ if __FILE__ == $0
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class PPSingleLineTest < Test::Unit::TestCase
|
||||
def test_hash
|
||||
assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699]
|
||||
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -94,32 +94,6 @@ non-string formatting, etc.
|
|||
--- flush
|
||||
outputs buffered data.
|
||||
|
||||
--- seplist(list[, separator_proc[, iter_method]]) {|elt| ... }
|
||||
adds a separated list.
|
||||
The list is separated by comma with breakable space, by default.
|
||||
|
||||
seplist iterates the ((|list|)) using ((|iter_method|)).
|
||||
It yields each object to the block given for seplist.
|
||||
The procedure ((|separator_proc|)) is called between each yields.
|
||||
|
||||
If the iteration is zero times, ((|separator_proc|)) is not called at all.
|
||||
|
||||
If ((|separator_proc|)) is nil or not given,
|
||||
(({lambda { comma_breakable }})) is used.
|
||||
If ((|iter_method|)) is not given, (({:each})) is used.
|
||||
|
||||
For example, following 3 code fragments has similar effect.
|
||||
|
||||
q.seplist([1,2,3]) {|v| xxx v }
|
||||
|
||||
q.seplist([1,2,3], lambda { comma_breakable }, :each) {|v| xxx v }
|
||||
|
||||
xxx 1
|
||||
q.comma_breakable
|
||||
xxx 2
|
||||
q.comma_breakable
|
||||
xxx 3
|
||||
|
||||
--- first?
|
||||
first? is obsoleted at 1.8.2.
|
||||
|
||||
|
@ -187,19 +161,6 @@ class PrettyPrint
|
|||
@group_stack.last
|
||||
end
|
||||
|
||||
def seplist(list, sep=nil, iter_method=:each)
|
||||
sep ||= lambda { comma_breakable }
|
||||
first = true
|
||||
list.__send__(iter_method) {|*v|
|
||||
if first
|
||||
first = false
|
||||
else
|
||||
sep.call
|
||||
end
|
||||
yield(*v)
|
||||
}
|
||||
end
|
||||
|
||||
def first?
|
||||
warn "PrettyPrint#first? is obsoleted at 1.8.2."
|
||||
current_group.first?
|
||||
|
|
Loading…
Add table
Reference in a new issue