非常教程

Ruby 2.4参考手册

完整输出 | PrettyPrint

PrettyPrint

父类:对象

这个类实现了一个好的打印算法。它为分组结构找到换行符和好的缩进。

默认情况下,该类假定基本元素是字符串,字符串中的每个字节都有单列宽度。但是通过给某些方法提供合适的参数,它可以用于其他情况:

  • newline对象和空间生成块:: new
  • #text的可选宽度参数
  • #breakable有几种候选用途:
  • 使用比例字体的文本格式
  • 具有与字节数不同的列的多字节字符
  • 非字符串格式

错误

  • 基于框的格式?
  • 其他(更好)的模型/算法?

报告bugs.ruby-lang.org上的任何错误

参考

Christian Lindig,严格漂亮,2000年3月,www.st.cs.uni-sb.de/~ lindig/papers/#pretty

Philip Wadler,一个好的打印机,1998年3月,homepages.inf.ed.ac.uk/ wadler/topics/language-design.html#prettier

作者

Tanaka Akira akr@fsij.org

属性

genspaceR

一个lambda或Proc,它接受Fixnum的一个参数,并返回相应数量的空格。

默认情况下这是:

lambda {|n| ' ' * n}

group_queueR

相当印刷的堆栈中的PrettyPrint :: GroupQueue

indentR

要缩进的空格数量

maxwidthR

在将行分隔到换行符之前,行的最大宽度

这默认为79,应该是一个Fixnum

newlineR

附加output到添加新行的值。

这个默认为“n”,应该是String

outputR

输出对象。

这个默认为'',并且应该接受<<方法

公共类方法

format(output =''。dup,maxwidth = 79,newline =“\ n”,genspace = lambda {| n |''* n}){| q | ...}显示源文件

这是一个方便的方法,它与以下相同:

begin
  q = PrettyPrint.new(output, maxwidth, newline, &genspace)
  ...
  q.flush
  output
end
# File lib/prettyprint.rb, line 44
def PrettyPrint.format(output=''.dup, maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
  q = PrettyPrint.new(output, maxwidth, newline, &genspace)
  yield q
  q.flush
  output
end

new(output =''。dup,maxwidth = 79,newline =“\ n”,&genspace)显示源文件

为好的打印创建一个缓冲区。

output是输出目标。如果没有指定,则假定为''。它应该有一个<<方法,它接受obj#text的第一个参数sep,#breakable的第一个参数newline,:: new 的第一个参数,以及:: new的给定块的结果。

maxwidth指定最大行长度。如果没有指定,则假定为79。但是,maxwidth如果提供长时间不可破坏的文本,实际输出可能会溢出。

newline用于换行符。如果未指定,则使用“n”。

该块用于生成空格。{|宽度| ''* width}被使用,如果没有给出。

# File lib/prettyprint.rb, line 81
def initialize(output=''.dup, maxwidth=79, newline="\n", &genspace)
  @output = output
  @maxwidth = maxwidth
  @newline = newline
  @genspace = genspace || lambda {|n| ' ' * n}

  @output_width = 0
  @buffer_width = 0
  @buffer = []

  root_group = Group.new(0)
  @group_stack = [root_group]
  @group_queue = GroupQueue.new(root_group)
  @indent = 0
end

singleline_format(output =''dup,maxwidth = nil,newline = nil,genspace = nil){| q | ...}显示源文件

这与::格式类似,但结果没有中断。

maxwidthnewline并被genspace忽略。

breakable块中的调用不会中断一行,只会被视为一个调用text

# File lib/prettyprint.rb, line 58
def PrettyPrint.singleline_format(output=''.dup, maxwidth=nil, newline=nil, genspace=nil)
  q = SingleLine.new(output)
  yield q
  output
end

公共实例方法

break_outmost_groups()显示源文件

将缓冲区分成短于最大宽度的行

# File lib/prettyprint.rb, line 159
def break_outmost_groups
  while @maxwidth < @output_width + @buffer_width
    return unless group = @group_queue.deq
    until group.breakables.empty?
      data = @buffer.shift
      @output_width = data.output(@output, @output_width)
      @buffer_width -= data.width
    end
    while !@buffer.empty? && Text === @buffer.first
      text = @buffer.shift
      @output_width = text.output(@output, @output_width)
      @buffer_width -= text.width
    end
  end
end

breakable(sep=' ', width=sep.length) Show source

这说“如果需要,你可以在这里打破一条线”,并且如果一条线在这一点上没有被破坏,则插入一个width列文本sep

如果sep未指定,则使用“”。

如果width未指定,sep.length则使用。sep例如,当你是一个多字节字符时,你将不得不指定它。

# File lib/prettyprint.rb, line 223
def breakable(sep=' ', width=sep.length)
  group = @group_stack.last
  if group.break?
    flush
    @output << @newline
    @output << @genspace.call(@indent)
    @output_width = @indent
    @buffer_width = 0
  else
    @buffer << Breakable.new(sep, width, self)
    @buffer_width += width
    break_outmost_groups
  end
end

current_group() Show source

返回最近添加到堆栈的组。

受影响的例子:

out = ""
=> ""
q = PrettyPrint.new(out)
=> #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0>
q.group {
  q.text q.current_group.inspect
  q.text q.newline
  q.group(q.current_group.depth + 1) {
    q.text q.current_group.inspect
    q.text q.newline
    q.group(q.current_group.depth + 1) {
      q.text q.current_group.inspect
      q.text q.newline
      q.group(q.current_group.depth + 1) {
        q.text q.current_group.inspect
        q.text q.newline
      }
    }
  }
}
=> 284
 puts out
#<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
#<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
#<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
#<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
# File lib/prettyprint.rb, line 154
def current_group
  @group_stack.last
end

fill_breakable(sep=' ', width=sep.length) Show source

除了决定是否决定破解决定外,这与可破密的类似。

一个组下的两个fill_breakable可能会导致4个结果:(break,break),(break,non-break),(non-break,break),(non-break,non-break)。这与易碎性不同,因为组下的两个易碎可能会导致2个结果:(break,break),(non-break,non-break)。

sep如果一行在这一点上没有被破解,则插入文本。

如果sep未指定,则使用“”。

如果width未指定,sep.length则使用。sep例如,当你是一个多字节字符时,你将不得不指定它。

# File lib/prettyprint.rb, line 211
def fill_breakable(sep=' ', width=sep.length)
  group { breakable sep, width }
end

flush() Show source

输出缓冲数据。

# File lib/prettyprint.rb, line 287
def flush
  @buffer.each {|data|
    @output_width = data.output(@output, @output_width)
  }
  @buffer.clear
  @buffer_width = 0
end

group(indent = 0,open_obj ='',close_obj ='',open_width = open_obj.length,close_width = close_obj.length){ ...}显示源文件

在该块中添加组线断点提示。换行提示全部使用或不使用。

如果indent指定,则方法调用被视为嵌套(缩进){...}。

如果open_obj指定,text open_obj, open_width则在分组之前调用。如果close_obj指定,text close_obj, close_width则在分组后调用。

# File lib/prettyprint.rb, line 248
def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
  text open_obj, open_width
  group_sub {
    nest(indent) {
      yield
    }
  }
  text close_obj, close_width
end

group_sub() { || ... } Show source

取一个块并排队一个进一步缩进1级的新组。

# File lib/prettyprint.rb, line 259
def group_sub
  group = Group.new(@group_stack.last.depth + 1)
  @group_stack.push group
  @group_queue.enq group
  begin
    yield
  ensure
    @group_stack.pop
    if group.breakables.empty?
      @group_queue.delete group
    end
  end
end

nest(indent) { || ... } Show source

换行符后增加左边距indent,在块中添加换行符。

# File lib/prettyprint.rb, line 276
def nest(indent)
  @indent += indent
  begin
    yield
  ensure
    @indent -= indent
  end
end

text(obj, width=obj.length) Show source

这增加objwidth宽度列的文本。

如果width未指定,则使用obj.length。

# File lib/prettyprint.rb, line 179
def text(obj, width=obj.length)
  if @buffer.empty?
    @output << obj
    @output_width += width
  else
    text = @buffer.last
    unless Text === text
      text = Text.new
      @buffer << text
    end
    text.add(obj, width)
    @buffer_width += width
    break_outmost_groups
  end
end

完整输出 | PrettyPrint相关

Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct