什么是“p”?在鲁比?
我确信对于那些知道的人来说这是一个愚蠢的问题,但我找不到它的作用或它是什么的解释。
CSV.open('data.csv', 'r') do |row|
p row
end
“p row
”有什么作用?
I'm sure it's a silly question to those who know, but I can't find an explanation of what it does or what it is.
CSV.open('data.csv', 'r') do |row|
p row
end
What does "p row
" do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
p()
是一个内核方法,它将
obj.inspect
写入标准输出。由于 Object 混合在 Kernel 模块中,因此
p()
方法随处可用。顺便说一句,在诗歌模式下使用它是很常见的,这意味着括号被删除。 CSV 片段可以写成...
它记录在 此处内核模块的其余部分。
p()
is a Kernel methodIt writes
obj.inspect
to the standard output.Because Object mixes in the Kernel module, the
p()
method is available everywhere.It's common, btw, to use it in poetry mode, meaning that the parens are dropped. The CSV snippet can be written like...
It's documented here with the rest of the Kernel module.
Kernel#p
是Kernel#puts
的小调试兄弟:它的工作原理或多或少与它完全相同,但它使用#inspect
转换其参数> 而不是#to_s
。它之所以有这样一个神秘的名字,是为了让你可以快速地将它扔进一个表达式中,并在调试时再次取出它。 (我想现在 Ruby 的“适当”调试支持越来越好,它的用处已经不大了。)
Kernel#p
的一些替代品是Kernel#pp
(漂亮的打印) ) 来自pp
标准库和Kernel#y
(YAML) 来自yaml
标准库。Kernel#p
is the little debugging brother ofKernel#puts
: it more or less works exactly like it, but it converts its arguments using#inspect
instead of#to_s
.The reason why it has such a cryptic name is so that you can quickly throw it into an expression and take it out again when debugging. (I guess it's a lot less useful now that Ruby is getting better and better "proper" debugging support.)
Some alternatives to
Kernel#p
areKernel#pp
(pretty print) from thepp
standard library andKernel#y
(YAML) from theyaml
standard library.为什么不尝试一下呢?
Why not try it?
您系统上已有的文档的另一个选项是 ri 命令。您可以随时键入:
ri p
,或者如果在很多地方(确实如此)为中央命令定义了p
,您可以尝试ri Kernel。 p
。其他不错的选择是Array.
或String.
。如果您最终安装了一堆 gem,这会减慢很多,但您可以查找 fastri gem,这会极大地加快查找过程。
The other option for documentation that you already have on your system is the
ri
command. At any time you can type:ri p
or ifp
is defined in a lot of places (which it is) for central commands you can tryri Kernel.p
. Other good bets areArray.<whatever method>
orString.<whatever method>
.If you end up installing a bunch of gems this will slow down a lot but you can look up the fastri gem which speeds up the lookup process incredibly.
Kernel#p 不如 print 和 put 广为人知。
它与 put 类似,它添加了换行符,但 p 调用的是 inform,而不是调用 to_s。
参考文献
http://garethirds.co .uk/2013/05/04/p-vs-puts-vs-print-in-ruby/
Kernel#p is less well known than print and puts.
It is similar to puts in that it adds a newline, but rather than calling to_s, p calls inspect.
References
http://garethrees.co.uk/2013/05/04/p-vs-puts-vs-print-in-ruby/
要理解 p 和 put 之间的区别,您必须理解 to_s() 和 instance() 方法之间的区别。
to_s 用于获取对象的字符串表示形式,而 instance 是对开发人员更友好的版本to_s 也给出了对象的内容。
To understand the difference between p and puts, you must understand difference between to_s() and instance() methods.
to_s is used to get string representation of an object while instance is a more developer friendly version of to_s which gives contents of the objects as well.