如何在服务器日志中查看有关对象的详细信息?

时间:2022-03-20 00:11:57

I am trying to debug the user object created by writing ruby code like

我试图调试通过编写ruby代码创建的用户对象

puts user

which then I can check it on the server log.

然后我可以在服务器日志上检查它。

Apparently, the server log says something like

显然,服务器日志说的是这样的

#<User:0x3b53440>

but it does not show details about the user object. (for example, its name or email values)

但它不显示有关用户对象的详细信息。 (例如,其名称或电子邮件值)

How should I modify the code so that the detail information about object will be produced?

我应该如何修改代码,以便生成有关对象的详细信息?

I want some function in ruby that does similar job as PHP's print_r or var_dump.

我想在ruby中使用一些与PHP的print_r或var_dump类似的函数。

2 个解决方案

#1


3  

Try using the Object.inspect method:

尝试使用Object.inspect方法:

puts user.inspect

Here's the documentation: http://www.ruby-doc.org/core/classes/Object.html#M001025

这是文档:http://www.ruby-doc.org/core/classes/Object.html#M001025

#2


0  

Often I'll write my own inspect or to_s method for an object, to provide me the view into the object that I want.

通常我会为对象编写自己的inspect或to_s方法,为我提供我想要的对象的视图。

If Ruby can't find either of those methods for an object, it'll return the object's ID, and nothing more, because it doesn't know what else to do.

如果Ruby无法为对象找到这些方法中的任何一个,它将返回对象的ID,仅此而已,因为它不知道还能做什么。

#1


3  

Try using the Object.inspect method:

尝试使用Object.inspect方法:

puts user.inspect

Here's the documentation: http://www.ruby-doc.org/core/classes/Object.html#M001025

这是文档:http://www.ruby-doc.org/core/classes/Object.html#M001025

#2


0  

Often I'll write my own inspect or to_s method for an object, to provide me the view into the object that I want.

通常我会为对象编写自己的inspect或to_s方法,为我提供我想要的对象的视图。

If Ruby can't find either of those methods for an object, it'll return the object's ID, and nothing more, because it doesn't know what else to do.

如果Ruby无法为对象找到这些方法中的任何一个,它将返回对象的ID,仅此而已,因为它不知道还能做什么。