Ruby 相当于 Perl Data::Dumper

发布于 2024-08-19 17:12:33 字数 111 浏览 2 评论 0原文

我正在学习 Ruby & Perl 有一个非常方便的模块,称为 Data::Dumper,它允许您递归地分析数据结构(如哈希)和数据结构。允许您打印它。这在调试时非常有用。 Ruby 有类似的东西吗?

I am learning Ruby & Perl has this very convenient module called Data::Dumper, which allows you to recursively analyze a data structure (like hash) & allow you to print it. This is very useful while debugging. Is there some thing similar for Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

对你的占有欲 2024-08-26 17:12:33

查看 pp

示例:

  require 'pp'
  x = { :a => [1,2,3, {:foo => bar}]}
  pp x

还有一个 inform 方法,它也工作得很好

  x = { :a => [1,2,3, {:foo => bar}]}
  puts x.inspect

Look into pp

example:

  require 'pp'
  x = { :a => [1,2,3, {:foo => bar}]}
  pp x

there is also the inspect method which also works quite nicely

  x = { :a => [1,2,3, {:foo => bar}]}
  puts x.inspect
想念有你 2024-08-26 17:12:33

如果我需要快速检查某些内容,我通常会使用 YAML 转储。

irb中,语法很简单y obj_to_inspect。在普通的 Ruby 应用程序中,您可能需要向文件添加 require 'YAML',但不确定。

以下是 irb 中的示例:

>> my_hash = {:array => [0,2,5,6], :sub_hash => {:a => 1, :b => 2}, :visible => true}
=> {:sub_hash=>{:b=>2, :a=>1}, :visible=>true, :array=>[0, 2, 5, 6]}
>> y my_hash  # <----- THE IMPORTANT LINE
--- 
:sub_hash: 
  :b: 2
  :a: 1
:visible: true
:array: 
- 0
- 2
- 5
- 6
=> nil
>> 

最终的=> nil 只是意味着该方法没有返回任何内容。它与你的数据结构无关。

I normally use a YAML dump if I need to quickly check something.

In irb the syntax is simply y obj_to_inspect. In a normal Ruby app, you may need to add a require 'YAML' to the file, not sure.

Here is an example in irb:

>> my_hash = {:array => [0,2,5,6], :sub_hash => {:a => 1, :b => 2}, :visible => true}
=> {:sub_hash=>{:b=>2, :a=>1}, :visible=>true, :array=>[0, 2, 5, 6]}
>> y my_hash  # <----- THE IMPORTANT LINE
--- 
:sub_hash: 
  :b: 2
  :a: 1
:visible: true
:array: 
- 0
- 2
- 5
- 6
=> nil
>> 

The final => nil just means the method didn't return anything. It has nothing to do with your data structure.

栩栩如生 2024-08-26 17:12:33

您可以使用MarshalmarshalYAML

you can use Marshal, amarshal, YAML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文