Class: Fiddle::CStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fiddle/struct.rb

Overview

A base class for objects representing a C structure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity_classObject

accessor to Fiddle::CStructEntity



12
13
14
# File 'lib/fiddle/struct.rb', line 12

def CStruct.entity_class
  CStructEntity
end

Instance Method Details

#eachObject



16
17
18
19
20
21
22
# File 'lib/fiddle/struct.rb', line 16

def each
  return enum_for(__function__) unless block_given?

  self.class.members.each do |name,|
    yield(self[name])
  end
end

#each_pairObject



24
25
26
27
28
29
30
# File 'lib/fiddle/struct.rb', line 24

def each_pair
  return enum_for(__function__) unless block_given?

  self.class.members.each do |name,|
    yield(name, self[name])
  end
end

#replace(another) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fiddle/struct.rb', line 40

def replace(another)
  if another.nil?
    self.class.members.each do |name,|
      self[name] = nil
    end
  elsif another.respond_to?(:each_pair)
    another.each_pair do |name, value|
      self[name] = value
    end
  else
    another.each do |name, value|
      self[name] = value
    end
  end
  self
end

#to_hObject



32
33
34
35
36
37
38
# File 'lib/fiddle/struct.rb', line 32

def to_h
  hash = {}
  each_pair do |name, value|
    hash[name] = unstruct(value)
  end
  hash
end