Ruby equivalent of Perl Data::Dumper

一笑奈何 提交于 2019-12-02 22:45:31

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

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.

you can use Marshal, amarshal, YAML

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!