Module: IOStruct::ClassMethods

Defined in:
lib/iostruct.rb

Instance Method Summary collapse

Instance Method Details

#nameObject



75
76
77
# File 'lib/iostruct.rb', line 75

def name
  'struct'
end

#read(src, size = nil) ⇒ Object

src can be IO or String, or anything that responds to :read or :unpack



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/iostruct.rb', line 60

def read(src, size = nil)
  pos = nil
  size ||= const_get 'SIZE'
  data =
    if src.respond_to?(:read)
      pos = src.tell
      src.read(size).to_s
    elsif src.respond_to?(:unpack)
      src
    else
      raise "[?] don't know how to read from #{src.inspect}"
    end
  new(*data.unpack(const_get('FORMAT'))).tap { |x| x.__offset = pos }
end

#sizeObject



79
80
81
# File 'lib/iostruct.rb', line 79

def size
  self::SIZE
end