Ruby 仿 C 结构体:CStruct 的一些例子
CStruct 是 Ruby 语言用来模仿 C 语言结构体的项目。[代码] 最简单的例子# CStruct Examples
require 'cstruct'
# example:
# struct Point in C\C++ (32-bit platform):
#
# struct Point
# {
# int x;
# int y;
# };
# struct Point in Ruby:
class Point < CStruct
int32:x
int32:y
end
# create a Point's instance
point = Point.new
# assign like as C language
point.x = 10
point.y = 20
puts "point.x = #{point.x},point.y = #{point.y}"
# struct PointF in C\C++ (32-bit platform):
#
# struct PointF
# {
# double x;
# double y;
# };
# struct PointF in Ruby:
class PointF < CStruct
double:x
double:y
end
# create a PointF's instance
# use 'block' to initialize the fields
point2 = PointF.new do |st|
st.x = 10.56
st.y = 20.78
end
puts "sizeof(PointF) = #{PointF.size}"
puts "point2.x = #{point2.x},point2.y = #{point2.y}" [代码] 数组成员01 # CStruct Examples
02 require 'cstruct'
03
04 # example:
05 # struct T in C\C++ (32-bit platform):
06 #
07 # struct T
08 # {
09 # int element;
10 # };
11
12 # struct T in Ruby:
13 class T < CStruct
14 int32:elements,
15 end
16
17 # create a T's instance
18 t_array = T.new
19
20 (0..7).each do |i|
21 t_array.elements = i# assign like as C language
22 end
23
24 # output
25 (0..7).each {|i| puts "t_array.elements[#{i}] = #{t_array.elements}" }
26
27
28 # Actually,t_array.elements.class is Array. So..
29 t_array.elements.each {|element| puts element } [代码] 结构体嵌套01 # CStruct Examples
02 require 'cstruct'
03 # example:
04 # struct A in C\C++ (32-bit platform):
05 # struct A{
06 # struct Inner
07 # {
08 # int v1;
09 # int v2;
10 # };
11 # Inner inner;
12 # };
13
14 # struct A in Ruby:
15 class A < CStruct
16 class Inner < CStruct
17 int32 :v1
18 int32 :v2
19 end
20 Inner :inner
21 end
22
23 a = A.new
24 a.inner.v1 = 1
25 a.inner.v2 = 2
26
27 puts a.inner.v1
28 puts a.inner.v2 [代码] 匿名结构体01 # CStruct Examples
02 require 'cstruct'
03
04 # example:
05 # struct Window in C\C++ (32-bit platform):
06 #
07 # struct Window
08 # {
09 # int style;
10 # struct{
11 # int x;
12 # int y;
13 # }position; /* position is anonymous struct's instance */
14 # };
15
16
17 # struct Window in Ruby:
18 class Window < CStruct
19 int32:style
20 struct :position do
21 int32:x
22 int32:y
23 end
24 end
25 # or like this (use brace):
26 # class Window < CStruct
27 # int32:style
28 # struct (:position) {
29 # int32:x
30 # int32:y
31 # }
32 # end
33
34 # create a Window's instance
35 window = Window.new
36
37 # assign like as C language
38 window.style = 1
39 window.position.x = 10
40 window.position.y = 10
41
42 puts "sizeof(Window) = #{Window.__size__}" # "__size__" is alias of "size"
43 puts "window.style = #{window.style},window.position.x = #{window.position.x},window.position.y = #{window.position.y}" [代码] 命名空间01 # CStruct Examples
02 require 'cstruct'
03
04 # example:
05 module NS1 #namespace
06 class A < CStruct
07 uint32:handle
08 end
09
10 module NS2
11 class B < CStruct
12 A:a # directly use A
13 end
14 end
15
16 class C < CStruct
17 A :a
18 NS2_B :b # Meaning of the 'NS2_B' is NS2::B
19 end
20 end
21
22 class D < CStruct
23 NS1_NS2_B:b # Meaning of the 'NS1_NS2_B' is NS1::NS2::B
24 end
25
26 v = D.new
27 v.b.a.handle = 120
28 p D.__size__
29 p v.b.a.handle
本帖最后由 bugbugbug3 于 2011-01-15 22:29 编辑
CStruct可以很方便的来操作二进制数据。像C中的结构体。
CStruct的主页
http://cstruct.rubyforge.org/
例子:
http://cstruct.rubyforge.org/examples.html
这里还有2篇关于CStruct使用的文章(中文)
在Ruby中方便的调用Win32 API (使用windows-pr和CStruct)
纯Ruby创建Win32窗口
页:
[1]