2013-08-17 7 views
9

प्रिंटिंग के लिए पर्ल के "डेटा :: डूपर" के रूबी समकक्ष Ruby equivalent of Perl Data::Dumper का डुप्लिकेट नहीं है। यह सवाल 3.5 साल से अधिक पुराना है और इसलिए जांच करना है कि तब से रुबी में कोई नया विकल्प उपलब्ध है।गहरी नेस्टेड हैश/एरेज़

मैं रूबी के Dumper रूबी में समकक्ष की तलाश में हूं। मुझे परवाह नहीं है कि पर्पर पर्दे के पीछे क्या करता है। मैंने गहरे घोंसले वाले हैंश और पेर्ल में सरणी मुद्रित करने के लिए इसे बड़े पैमाने पर उपयोग किया है। अब तक मुझे रूबी में कोई विकल्प नहीं मिला है (या मुझे रूबी में उपलब्ध विकल्पों का अच्छा उपयोग करने का कोई तरीका नहीं मिला है)।

यह मेरा पर्ल कोड और उसके आउटपुट है:

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

बस प्यार डम्पर। :)

माणिक में, मैं pp, p, 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 

धन्यवाद यहाँ गहरे लाल रंग का और इसके उत्पादन में मेरी एक ही कोड है।

+0

क्या आप द्वारा * YAML इस परीक्षण कोड के लिए अच्छा है मतलब बल्कि बहुत ही गहन नेस्टेड संरचनाओं के लिए नहीं है * ? –

+3

यम आउटपुट दर्दनाक (मेरे लिए कम से कम) डूपर की तुलना में पढ़ने और व्याख्या करने के लिए है जब यह गहरी हैश/सरणी की बात आती है। – slayedbylucifer

+0

डाउनवॉट्स? किस लिए? सवाल के साथ क्या गलत है? – slayedbylucifer

उत्तर

9

Awesome Print के बारे में क्या:

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

आउटपुट (वाक्य रचना हाइलाइटिंग के साथ वास्तव में):

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

आह, awesome_print वास्तव में कमाल है। धन्यवाद @ स्टेफ़ान – slayedbylucifer

संबंधित मुद्दे