这不是重复的Ruby 相当于 Perl Data::Dumper https://stackoverflow.com/questions/2159426/ruby-equivalent-of-perl-datadumper。这个问题已经存在超过 3.5 年了,因此想检查一下从那时起 Ruby 中是否有任何新的选项可用。
我正在寻找 Perl 的Dumper
红宝石中的等价物。我不在乎Dumper在幕后做什么。我广泛使用它在 Perl 中打印深层嵌套的哈希值和数组。到目前为止,我还没有在 Ruby 中找到替代方案(或者我可能没有找到一种方法来充分利用 Ruby 中的可用替代方案)。
这是我的 Perl 代码及其输出:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $hash;
$hash->{what}->{where} = "me";
$hash->{what}->{who} = "you";
$hash->{which}->{whom} = "she";
$hash->{which}->{why} = "him";
print Dumper($hash);
OUTPUT:
$VAR1 = {
'what' => {
'who' => 'you',
'where' => 'me'
},
'which' => {
'why' => 'him',
'whom' => 'she'
}
};
只是喜欢倾销者。 :)
在红宝石中,我尝试过pp
, p
, inspect
and yaml
。这是我在 ruby 中的相同代码及其输出:
#!/usr/bin/ruby
require "pp"
require "yaml"
hash = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }
hash[:what][:where] = "me"
hash[:what][:who] = "you"
hash[:which][:whom] = "she"
hash[:which][:why] = "him"
pp(hash)
puts "Double p did not help. Lets try single p"
p(hash)
puts "Single p did not help either...lets do an inspect"
puts hash.inspect
puts "inspect was no better...what about yaml...check it out"
y hash
puts "yaml is good for this test code but not for really deep nested structures"
OUTPUT:
{:what=>{:where=>"me", :who=>"you"}, :which=>{:whom=>"she", :why=>"him"}}
Double p did not help. Lets try single p
{:what=>{:where=>"me", :who=>"you"}, :which=>{:whom=>"she", :why=>"him"}}
Single p did not help either...lets do an inspect
{:what=>{:where=>"me", :who=>"you"}, :which=>{:whom=>"she", :why=>"him"}}
inspect was no better...what about yaml...check it out
---
:what:
:where: me
:who: you
:which:
:whom: she
:why: him
yaml is good for this test code but not for really deep nested structures
Thanks.