File: anonymous_struct.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 851 bytes parent folder | download | duplicates (2)
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}"