Ruby Equivalent of Perl Data::Dumper

Ruby equivalent of Perl Data::Dumper

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

Ruby equivalent of perl's Data::Dumper for printing deep nested hashes/arrays

What about Awesome Print:

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

Output (actually with syntax highlighting):

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

What does the DumpXS in Perl's Data::Dumper do?

The XS language is a glue between normal Perl and C. When people want to squeeze every last bit of performance out of an operation, they try to write it as close to the C code as possible. Python and Ruby have similar mechanisms for the same reason.

Some Perl modules have an XS implementation to improve performance. However, you need a C compiler to install it. Not everyone is in a position to install compiled modules, so the modules also come in a "PurePerl" or "PP" version that does the same thing just a bit slower. If you don't have the XS implementation, a module such as Data::Dumper can automatically use the pure Perl implementation. In this case, Data::Dumper also lets you choose which one you want to use.

How to understand the output of Data::Dumper?

Data::Dumper needs to get a reference to a string without creating a new variable in the current scope, this is presumably how it does it. Working from the middle outwards we have my $o = 'test' which declares $o, sets it's value to 'test' and also returns $o. The do{} block in this case provides a scope for the my binding to exist in, when the block exits $o ceases to exist but the value it references continues to, which is good as the \ at the start of the do block takes a reference to it's returned value. The reference to the string 'test' is then blessed with 'Tk'.

What is Perl's equivalent to Ruby's Pry

There is this CPAN module available.

https://metacpan.org/pod/Pry

Is there a Go Language equivalent to Perls' Dumper() method in Data::Dumper?

I found a couple packages to help visualize data in Go.

My personal favourite - https://github.com/davecgh/go-spew

There's also - https://github.com/tonnerre/golang-pretty

Unexpected Data::Dumper::Dumper result

In your example $VAR1->{'AA'}[2] and $VAR1->{'BB'}[2] are references to the same hash.

Data::Dumper does not want to print a variable more than once. This behaviour represents the data structure more faithfully, and it avoids any infinite loop it might encounter.
e.g.:

my $loop;
$loop = { 1 => \$loop };
print Dumper $loop;

Output is

$VAR1 = {
'1' => \$VAR1
};


Related Topics



Leave a reply



Submit