这不是Perl Data::Dumper 的 Ruby 等价物的副本。这个问题已经超过 3.5 年了,因此想检查从那时起 Ruby 中是否有任何新的选项可用。
我正在寻找 perlDumper
在 ruby 中的等价物。我不在乎 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);
输出:
$VAR1 = {
'what' => {
'who' => 'you',
'where' => 'me'
},
'which' => {
'why' => 'him',
'whom' => 'she'
}
};
只是喜欢翻车机。:)
在 ruby 中,我尝试了pp
,p
和. 这是我在 ruby 中的相同代码及其输出:inspect
yaml
#!/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"
输出:
{: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
谢谢。