Data::Dumper for Ruby?

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

Do you actually need the syntax preserved, or are you just trying to
serialize data?
If the latter, use Marshal or the YAML std library, or consider JSON
(standard in 1.9, a gem in 1.8)

-greg

路路路

On Thu, Sep 3, 2009 at 11:00 AM, Josef Wolf<jw@raven.inka.de> wrote:

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

In Ruby we generally use the standard YAML library for that. If you don't need the content to be human readable, you could also use Marshal, part of Ruby's core. Ruby 1.9 also ships with a JSON library, which is available as a gem for Ruby 1.8.

Hope that helps.

James Edward Gray II

路路路

On Sep 3, 2009, at 10:00 AM, Josef Wolf wrote:

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

Josef Wolf wrote:

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

http://raa.ruby-lang.org/project/amarshal/

It's a bit old, but it does output ruby syntax.

路路路

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

An ex-perl hacker, I used to use Data::Dumper all the time. Now in
ruby, I find that I don't need the equivalent. I tend to think in
terms of classes and attributes instead of nested data structures. I
can poke around in my class structure in irb.

I think you'll eventually realize that in Perl, there is a tendency to
overuse data structures because it is the most convenient way of
storing data. In ruby there are better ways. This reminds me of an old
post of mine: markthomas.org
which, although not about Perl per se, covers the same issue.

路路路

On Sep 3, 11:00 am, Josef Wolf <j...@raven.inka.de> wrote:

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

Hello,

I am new to Ruby and am about to start my first application that exceeds the
size of gello.rb :wink:

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Anybody knows of a similar thing for ruby?

In Ruby we generally use the standard YAML library for that. If you don't need the content to be human readable, you could also use Marshal, part of Ruby's core. Ruby 1.9 also ships with a JSON library, which is available as a gem for Ruby 1.8.

Hope that helps.

James Edward Gray II

You might also be interested in the inspect method. It's what irb uses to show the last value, but you can call it yourself to get a string representation of standard data structures.

my_hash_of_arrays = { 'fibs' => [ 0, 1, 1, 2, 3, 5, 8, 13 ],

'primes' => [ 2, 3, 5, 7, 11, 13, 17, 19 ], 'squares' => [ 1, 4, 9, 16 ] }
=> {"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13], "squares"=>[1, 4, 9, 16], "primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}

my_hash_of_arrays.inspect

=> "{\"fibs\"=>[0, 1, 1, 2, 3, 5, 8, 13], \"squares\"=>[1, 4, 9, 16], \"primes\"=>[2, 3, 5, 7, 11, 13, 17, 19]}"

puts my_hash_of_arrays.inspect

{"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13], "squares"=>[1, 4, 9, 16], "primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}
=> nil

p my_hash_of_arrays

{"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13], "squares"=>[1, 4, 9, 16], "primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}
=> nil

You can also require the pretty print library.

require 'pp'

=> true

pp my_hash_of_arrays

{"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13],
  "squares"=>[1, 4, 9, 16],
  "primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}
=> nil

And you can get that value into your own string with the PP::pp method (and a StringIO to collect the "output")

require 'stringio'

=> true

my_string = StringIO.new

=> #<StringIO:0x394234>

PP::pp(my_hash_of_arrays, my_string)

=> #<StringIO:0x394234>

my_string.rewind

=> 0

my_string.read

=> "{\"fibs\"=>[0, 1, 1, 2, 3, 5, 8, 13],\n \"squares\"=>[1, 4, 9, 16],\n \"primes\"=>[2, 3, 5, 7, 11, 13, 17, 19]}\n"

my_string.rewind

=> 0

puts my_string.read

{"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13],
  "squares"=>[1, 4, 9, 16],
  "primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}
=> nil

And all of this is part of Ruby's standard library.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

路路路

On Sep 3, 2009, at 11:06 AM, James Edward Gray II wrote:

On Sep 3, 2009, at 10:00 AM, Josef Wolf wrote:

Thanks for the fast answers!

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Do you actually need the syntax preserved, or are you just trying to
serialize data?

The purpose is to check whether the data structures I have built (or am about
to build) are how they should be. So the important thing here is that it is
easy to grasp visually.

If the latter, use Marshal or the YAML std library, or consider JSON
(standard in 1.9, a gem in 1.8)

I know YAML, and I actually use it a lot for configuration or persistent
storage. But I find YAML somewhat hard to read and understand when the data
structures start getting complicated. With output from Data::Dumper, you
fire up emacs to easily find matching scopes.

路路路

On Fri, Sep 04, 2009 at 12:04:30AM +0900, Gregory Brown wrote:

On Thu, Sep 3, 2009 at 11:00 AM, Josef Wolf<jw@raven.inka.de> wrote:

try

require "pp"

pp my_obj

路路路

On Thu, Sep 3, 2009 at 11:40 AM, Josef Wolf<jw@raven.inka.de> wrote:

On Fri, Sep 04, 2009 at 12:04:30AM +0900, Gregory Brown wrote:

On Thu, Sep 3, 2009 at 11:00 AM, Josef Wolf<jw@raven.inka.de> wrote:

Thanks for the fast answers!

One of the first things I am missing is something like perl's Data::Dumper
module. This module is able to take a (nested) data structure and dump it
in perl syntax.

Do you actually need the syntax preserved, or are you just trying to
serialize data?

The purpose is to check whether the data structures I have built (or am about
to build) are how they should be. So the important thing here is that it is
easy to grasp visually.

[ ... ]

You can also require the pretty print library.

> require 'pp'
=> true
> pp my_hash_of_arrays
{"fibs"=>[0, 1, 1, 2, 3, 5, 8, 13],
"squares"=>[1, 4, 9, 16],
"primes"=>[2, 3, 5, 7, 11, 13, 17, 19]}
=> nil

Yeah, this looks very fine. Thanks!

路路路

On Fri, Sep 04, 2009 at 12:37:57AM +0900, Rob Biedenharn wrote:

On Sep 3, 2009, at 11:06 AM, James Edward Gray II wrote:

On Sep 3, 2009, at 10:00 AM, Josef Wolf wrote: