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
|
# CStruct Examples
require 'cstruct'
# example:
# struct Window in C\C++ (32-bit platform):
#
# struct Window
# {
# int style;
# struct{
# int x;
# int y;
# }position; /* position is anonymous struct's instance */
# };
# struct Window in Ruby:
class Window < CStruct
int32:style
struct :position do
int32:x
int32:y
end
end
# or like this (use brace):
# class Window < CStruct
# int32:style
# struct (:position) {
# int32:x
# int32:y
# }
# end
# create a Window's instance
window = Window.new
# assign like as C language
window.style = 1
window.position.x = 10
window.position.y = 10
puts "sizeof(Window) = #{Window.__size__}" # "__size__" is alias of "size"
puts "window.style = #{window.style},window.position.x = #{window.position.x},window.position.y = #{window.position.y}"
|