class Marshal::Structure

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

Constants

VERSION

Version of Marshal::Structure you are using

Attributes

stream[R]

The Marshal stream

Public Class Methods

load(obj) click to toggle source

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
new(stream) click to toggle source

Prepares processing of stream

# File lib/marshal/structure.rb, line 125
def initialize stream
  @stream    = stream
  @tokenizer = Marshal::Structure::Tokenizer.new stream
end
run(argv = ARGV) click to toggle source

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

Public Instance Methods

count_allocations() click to toggle source

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
load() click to toggle source

Loads the stream with Marshal.load

# File lib/marshal/structure.rb, line 144
def load
  Marshal.load @stream
end
structure() click to toggle source

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
token_stream() click to toggle source

Returns an Enumerator for the tokens in the Marshal stream.

# File lib/marshal/structure.rb, line 160
def token_stream
  @tokenizer.tokens
end