2013-08-17 8 views
9

Nie jest to duplikat Ruby equivalent of Perl Data::Dumper. Pytanie to ma więcej niż 3,5 lat, a więc chcesz sprawdzić, czy od tego czasu dostępne są nowe opcje dostępne w Ruby.Odpowiednik ruby ​​"Data :: Dumper" firmy Perl do drukowania głęboko zagnieżdżonych skrótów/tablic

Poszukuję odpowiednika perla w rublu w wersji Dumper. Nie obchodzi mnie, co Dumper robi za zasłonami. Użyłem go intensywnie do drukowania głębokich zagnieżdżonych skrótów i tablic w perlu. Do tej pory nie znalazłem alternatywy w ruby ​​(lub może nie znalazłem sposobu, aby dobrze wykorzystać dostępne opcje w Ruby).

To jest mój kod Perl, a jego produkcja:

#!/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); 

WYJŚCIE:

$VAR1 = { 
      'what' => { 
         'who' => 'you', 
         'where' => 'me' 
        }, 
      'which' => { 
         'why' => 'him', 
         'whom' => 'she' 
        } 
     }; 

prostu uwielbiam Wywrotka. :)

W rubinie próbowałem pp, p, inspect i yaml. tutaj jest mój sam kod w języku Ruby i jej wyjściu:

#!/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" 

WYJŚCIE:

{: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 

Dzięki.

+0

co masz na myśli przez * yaml jest dobre dla tego kodu testowego, ale nie za bardzo głębokich struktur zagnieżdżonych * ? –

+3

Wyjście yaml jest bolesne (przynajmniej dla mnie) do czytania i interpretowania w porównaniu do Wywrotka, jeśli chodzi o głębokie hasze/tablice. – slayedbylucifer

+0

downvotes? po co? co jest nie tak z pytaniem? – slayedbylucifer

Odpowiedz

9

Co Awesome Print:

require 'awesome_print' 
hash = {what: {where: "me", who: "you"}, which: { whom: "she", why: "him"}} 
ap hash 

wyjściowa (faktycznie z podświetlaniem składni):

{ 
    :what => { 
     :where => "me", 
      :who => "you" 
    }, 
    :which => { 
     :whom => "she", 
     :why => "him" 
    } 
} 
+0

Aah, awesome_print jest naprawdę niesamowity. Dzięki @Stefan – slayedbylucifer

Powiązane problemy