I have recently waffled between Haskell and Scala in terms of learning.  Well, I'm back on Scala. :) I've found that working with O...

January 16, 2014

Ruby's Data::Dumper

I was poking around a bit trying to find the Ruby equivalent of Perls Data::Dumper or Data::Printer.  At first, I didn't think anything like those existed.

Upon further research, it seems that the "normal" way of doing this is to use Marshal or YAML.

These are ok, and I'll probably play with them, but I got more excited when I subsequently discovered Awesome Print.

After installing...

gem install awesome_print

First run:

ctreptow@vm-0:~$ cat 1.rb                                                                                                                                                 
#!/usr/bin/env ruby
require "awesome_print"
data = [ false, 42, %w(forty two), { :now => Time.now,
 :class => Time.now.class, :distance => 42e42 } ]
ap data

ctreptow@vm-0:~$ ./1.rb                                                                                                                                                   
    [0] false,
    [1] 42,
    [2] [
        [0] "forty",
        [1] "two"
    [3] {
             :now => 2014-01-15 22:49:02 +0000,
           :class => Time < Object,
        :distance => 4.2e+43

I like that.  I think it will come in handy when I get around to actually making something.

No comments:

Post a Comment