什么是“p”?在鲁比?

发布于 2024-08-11 05:30:24 字数 157 浏览 3 评论 0原文

我确信对于那些知道的人来说这是一个愚蠢的问题,但我找不到它的作用或它是什么的解释。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

聊慰 2024-08-18 05:30:24

p() 是一个内核方法,

它将 obj.inspect 写入标准输出。

由于 Object 混合在 Kernel 模块中,因此 p() 方法随处可用。

顺便说一句,在诗歌模式下使用它是很常见的,这意味着括号被删除。 CSV 片段可以写成...

CSV.open 'data.csv', 'r' do |row|
  p row
end

它记录在 此处内核模块的其余部分。

p() is a Kernel method

It 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...

CSV.open 'data.csv', 'r' do |row|
  p row
end

It's documented here with the rest of the Kernel module.

死开点丶别碍眼 2024-08-18 05:30:24

Kernel#pKernel#puts 的小调试兄​​弟:它的工作原理或多或少与它完全相同,但它使用 #inspect 转换其参数> 而不是#to_s

它之所以有这样一个神秘的名字,是为了让你可以快速地将它扔进一个表达式中,并在调试时再次取出它。 (我想现在 Ruby 的“适当”调试支持越来越好,它的用处已经不大了。)

Kernel#p 的一些替代品是 Kernel#pp (漂亮的打印) ) 来自 pp 标准库和 Kernel#y (YAML) 来自 yaml 标准库。

Kernel#p is the little debugging brother of Kernel#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 are Kernel#pp (pretty print) from the pp standard library and Kernel#y (YAML) from the yaml standard library.

风吹过旳痕迹 2024-08-18 05:30:24

为什么不尝试一下呢?

>> [1,2,3].each { |d| p d }
1
2
3

Why not try it?

>> [1,2,3].each { |d| p d }
1
2
3
只涨不跌 2024-08-18 05:30:24

您系统上已有的文档的另一个选项是 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 if p is defined in a lot of places (which it is) for central commands you can try ri Kernel.p. Other good bets are Array.<whatever method> or String.<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.

谁的年少不轻狂 2024-08-18 05:30:24

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/

ぇ气 2024-08-18 05:30:24

要理解 p 和 put 之间的区别,您必须理解 to_s() 和 instance() 方法之间的区别。

to_s 用于获取对象的字符串表示形式,而 instance 是对开发人员更友好的版本to_s 也给出了对象的内容。

class Dog
        def initialize(name, breed)
            @name = name
            @breed = breed
        end
        def to_s
            puts  "#@name's breed is #@breed."
        end
end

terra=Dog.new("Terra","Husky")
puts terra #Terra's breed is Husky.
p terra    #<Dog:0x00007fbde0932a88 @name="Terra", @breed="Husky">  

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.

class Dog
        def initialize(name, breed)
            @name = name
            @breed = breed
        end
        def to_s
            puts  "#@name's breed is #@breed."
        end
end

terra=Dog.new("Terra","Husky")
puts terra #Terra's breed is Husky.
p terra    #<Dog:0x00007fbde0932a88 @name="Terra", @breed="Husky">  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文