Class: Achoo::System::CStruct
- Inherits:
-
Object
- Object
- Achoo::System::CStruct
- Defined in:
- lib/achoo/system/cstruct.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
- .bin_size ⇒ Object
- .char(name) ⇒ Object
- .inherited(subclass) ⇒ Object
- .long(name) ⇒ Object
- .quad(name) ⇒ Object
- .short(name) ⇒ Object
- .string(name, length) ⇒ Object
Instance Method Summary collapse
-
#initialize(bytes = nil) ⇒ CStruct
constructor
A new instance of CStruct.
- #pack ⇒ Object
- #unpack(str) ⇒ Object
Constructor Details
#initialize(bytes = nil) ⇒ CStruct
Returns a new instance of CStruct.
8 9 10 11 |
# File 'lib/achoo/system/cstruct.rb', line 8 def initialize(bytes=nil) @values = [] unpack(bytes) unless bytes.nil? end |
Class Attribute Details
.template ⇒ Object (readonly)
Returns the value of attribute template.
15 16 17 |
# File 'lib/achoo/system/cstruct.rb', line 15 def template @template end |
Class Method Details
.bin_size ⇒ Object
31 32 33 34 35 |
# File 'lib/achoo/system/cstruct.rb', line 31 def bin_size @bin_size ||= template.split('').select {|c| c =~ /[[:alpha:]]/}.map do |c| c == 'A' ? '' : 0 end.pack(template).length end |
.char(name) ⇒ Object
22 |
# File 'lib/achoo/system/cstruct.rb', line 22 def char(name); add_type(name, :char, 'c', 0); end |
.inherited(subclass) ⇒ Object
17 18 19 20 |
# File 'lib/achoo/system/cstruct.rb', line 17 def inherited(subclass) subclass.instance_variable_set(:@template, '') subclass.instance_variable_set(:@count, 0) end |
.long(name) ⇒ Object
24 |
# File 'lib/achoo/system/cstruct.rb', line 24 def long(name); add_type(name, :long, 'l', 0); end |
.quad(name) ⇒ Object
25 |
# File 'lib/achoo/system/cstruct.rb', line 25 def quad(name); add_type(name, :quad, 'q', 0); end |
.short(name) ⇒ Object
23 |
# File 'lib/achoo/system/cstruct.rb', line 23 def short(name); add_type(name, :short, 's', 0); end |
.string(name, length) ⇒ Object
27 28 29 |
# File 'lib/achoo/system/cstruct.rb', line 27 def string(name, length) add_type(name, :string, 'A', '', length) end |
Instance Method Details
#pack ⇒ Object
60 61 62 63 |
# File 'lib/achoo/system/cstruct.rb', line 60 def pack t = self.class.template.tr('A', 'a') @values.pack(t) end |
#unpack(str) ⇒ Object
56 57 58 |
# File 'lib/achoo/system/cstruct.rb', line 56 def unpack(str) @values = str.unpack(self.class.template) end |