Module: IOStruct

Defined in:
lib/iostruct.rb,
lib/iostruct/version.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.new(fmt, *args) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/iostruct.rb', line 2

def self.new fmt, *args
  size = fmt.scan(/([a-z])(\d*)/i).map do |f,len|
    [len.to_i, 1].max *
      case f
      when /[AaCcx]/ then 1
      when 'v' then 2
      when 'V','l','L' then 4
      when 'Q' then 8
      else raise "unknown fmt #{f.inspect}"
      end
  end.inject(&:+)

  Struct.new( *args ).tap do |x|
    x.const_set 'FORMAT', fmt
    x.const_set 'SIZE',  size
    x.class_eval do
      include InstanceMethods
    end
    x.extend ClassMethods
  end
end