Ruby 检查可读性吗?

有没有办法结论 inspect 更可读?

在 Perl 有
Data::Dumper

, 是什么让输出更容易阅读。
已邀请:

郭文康

赞同来自:

标准库 ruby 包含 PP /减少 "Pretty Print"/, 哪种格式化结构比标准更好 inspect:

http://www.ruby-doc.org/stdlib ... .html
你需要
require 'pp'

在源文件的顶部,在您可以使用它之前,然后只替换
p obj


pp obj

在你的代码中。

我觉得特别适用于HAATE和数组!

喜特乐

赞同来自:

决定旁边 'pp' 可能是一个解决方案 yaml.

尝试:


require 'yaml'
puts [1,2,3=>'three'].to_yaml


你得到:


--- 
- 1
- 2
- 3: three


和一个完全不同的方法:如果遇到问题,请写自己的检查代码 inspect - 与特定班级

例子:


class MyTest
def initialize//
@created = Time.now
end
def inspect//
"Hi, I'm the objected created at #{@created}. That's #{Time.now - @created} seconds ago"
end
end

x = MyTest.new
sleep 5
puts x.inspect


你得到


Hi, I'm the objected created at 2011-06-28 12:48:38 +0100. That's 5.0 seconds ago

冰洋

赞同来自:

据说 @tjbp, 刚使用
pretty_inspect//

反而
inspect//


它会给你一个好的线条突破和缩进

二哥

赞同来自:

它会工作,看起来很棒 :/

在您的文件中:


group :development do
gem 'table_print', '~> 1.5.3'
end


依你的意见:


<pre>
&lt;%= TablePrint::Printer.new/@users, []/.table_print %&gt;
</pre>

要回复问题请先登录注册