Marshal::Structure dumps a nested Array describing the structure of a Marshal stream. Marshal format 4.8 (Ruby 1.8 through 2.x) is supported.
Examples:
To dump the structure of a Marshal stream:
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 stream structure pp ms.structure # show how many allocations are required to load the stream p ms.count_allocations
Version of Marshal::Structure you are using
The Marshal stream
Returns the structure of the Marshaled object obj
as nested
Arrays.
For true
, false
and nil
the symbol
:true
, :false
, :nil
is returned,
respectively.
For Fixnum the value is returned.
For other objects the first item is the reference for future occurrences of the object and the remaining items describe the object.
Symbols have a separate reference table from all other objects.
# File lib/marshal/structure.rb, line 85 def self.load obj if obj.respond_to? :to_str then data = obj.to_s elsif obj.respond_to? :read then data = obj.read raise EOFError, "end of file reached" if data.empty? elsif obj.respond_to? :getc then # FIXME - don't read all of it upfront data = '' while c = obj.getc do data << c.chr end else raise TypeError, "instance of IO needed" end new(data).structure end
Prepares processing of stream
# File lib/marshal/structure.rb, line 125 def initialize stream @stream = stream @tokenizer = Marshal::Structure::Tokenizer.new stream end
Dumps the structure of each item in argv
. If
argv
is empty standard input is dumped.
# File lib/marshal/structure.rb, line 108 def self.run argv = ARGV require 'pp' if argv.empty? then pp load $stdin else argv.each do |file| open file, 'rb' do |io| pp load io end end end end
Counts allocations required to load the Marshal stream. See Marshal::Structure::AllocationsCounter for a description of how counting is performed.
# File lib/marshal/structure.rb, line 135 def count_allocations counter = Marshal::Structure::AllocationCounter.new token_stream counter.count end
Loads the stream with Marshal.load
# File lib/marshal/structure.rb, line 144 def load Marshal.load @stream end
Returns the structure of the Marshal stream.
# File lib/marshal/structure.rb, line 151 def structure parser = Marshal::Structure::Parser.new token_stream parser.parse end
Returns an Enumerator for the tokens in the Marshal stream.
# File lib/marshal/structure.rb, line 160 def token_stream @tokenizer.tokens end