marshal-structure 1.0

drbrain | Sat, 06 Aug 2011 00:51:59 GMT

Posted in ,

This gem is part of #rbxday, but instead of contributing to Rubinius, it mostly takes from Rubinius!

marshal-structure dumps a tree based on the Marshal format. It supports the Marshal 4.8 format.

INSTALL

gem install marshal-structure

SYNOPSIS

From the command line:

ruby -rpp -rmarshal-structure \
  -e 'pp Marshal::Structure.load Marshal.dump "hello"'

Fancier usage:

require 'pp'
require 'marshal-structure'

ms = Marshal::Structure.new Marshal.dump %w[hello world]

# print the Marshal stream structure
pp ms.construct

# print ruby objects in Marshal stream
pp ms.objects

EXAMPLE

str =
  "\004\b{\006:\006a[\031c\006Bm\006C\"\006d/\006e\000i\006" \
  "f\0322.2999999999999998\000ff" \
  "l+\n\000\000\000\000\000\000\000\000\001\0000TF}\000i\000" \
  "S:\006S\006:\006fi\000o:\vObject\000@\017" \
  "U:\006M\"\021marshal_dump" \
  "Iu:\006U\n_dump\006" \
  ":\026@ivar_on_dump_str\"\036value on ivar on dump str" \
  ";\000e:\006Eo;\b\000" \
  "I\"\025string with ivar\006:\v@value\"\017some value" \
  "C:\016BenString\"\000"

structure = Marshal::Structure.load str

pp structure

Prints:

[:hash,
 0,
 1,
 [:symbol, 0, "a"],
 [:array,
  1,
  20,
  [:class, 2, "B"],
  [:module, 3, "C"],
  [:string, 4, "d"],
  [:regexp, 5, "e", 0],
  [:fixnum, 1],
  [:float, 6, "2.2999999999999998\u0000ff"],
  [:bignum, 7, 1, 10, 18446744073709551616],
  :nil,
  :true,
  :false,
  [:hash_default, 8, 0, [:fixnum, 0]],
  [:struct, 9, [:symbol, 1, "S"], 1, [:symbol, 2, "f"], [:fixnum, 0]],
  [:object, 10, [:symbol, 3, "Object"], [0]],
  [:link, 10],
  [:user_marshal, 11, [:symbol, 4, "M"], [:string, 12, "marshal_dump"]],
  [:instance_variables,
   [:user_defined, 13, [:symbol, 5, "U"], "_dump"],
   1,
   [:symbol, 6, "@ivar_on_dump_str"],
   [:string, 14, "value on ivar on dump str"]],
  [:symbol_link, 0],
  [:extended, [:symbol, 7, "E"], [:object, 15, [:symbol_link, 3], [0]]],
  [:instance_variables,
   [:string, 16, "string with ivar"],
   1,
   [:symbol, 8, "@value"],
   [:string, 17, "some value"]],
  [:user_class, [:symbol, 9, "BenString"], [:string, 18, ""]]]]
comment

Comments RSS FEED

Comments are disabled