1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
# CStruct Examples
require 'cstruct'
# example:
# struct Point in C\C++ (32-bit platform):
#
# struct Point
# {
# int x;
# int y;
# };
#
# struct Line in C\C++ (32-bit platform):
#
# struct Line
# {
# Point begin_point;
# Point end_point;
# };
# struct TwoLine in C\C++ (32-bit platform):
#
# struct TwoLine
# {
# Line lines[2];
# };
# struct Point , Line and TwoLine in Ruby:
class Point < CStruct
int32:x
int32:y
end
class Line < CStruct
Point:begin_point
Point:end_point
end
class TwoLine < CStruct
Line:lines,[2]
end
line = Line.new
# assign like as C language
line.begin_point.x = 0
line.begin_point.y = 0
line.end_point.x = 20
line.end_point.y = 30
puts "sizeof(Line) = #{Line.__size__}" # "__size__" is alias of "size"
puts "line.begin_point.x = #{line.begin_point.x}"
puts "line.begin_point.y = #{line.begin_point.y}"
puts "line.end_point.x = #{line.end_point.x}"
puts "line.end_point.y = #{line.end_point.y}"
two_line = TwoLine.new
# assign like as C language
two_line.lines[0].begin_point.x = 6
two_line.lines[0].begin_point.y = 16
two_line.lines[0].end_point.x = 26
two_line.lines[0].end_point.y = 36
two_line.lines[1].begin_point.x = 6
two_line.lines[1].begin_point.y = 16
two_line.lines[1].end_point.x = 26
two_line.lines[1].end_point.y = 36
|