免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3154 | 回复: 2
打印 上一主题 下一主题

Ruby 仿 C 结构体:CStruct 的一些例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-14 13:43 |只看该作者 |倒序浏览
CStruct 是 Ruby 语言用来模仿 C 语言结构体的项目。


[代码] 最简单的例子
  1. # CStruct Examples
  2. require 'cstruct'

  3. # example:
  4. # struct Point in C\C++        (32-bit platform):
  5. #
  6. # struct Point
  7. # {
  8. #    int x;
  9. #    int y;
  10. # };

  11. # struct Point in Ruby:
  12. class Point < CStruct
  13.   int32:x
  14.   int32:y
  15. end

  16. # create a Point's instance
  17. point   = Point.new

  18. # assign like as C language
  19. point.x = 10
  20. point.y = 20

  21. puts "point.x = #{point.x},point.y = #{point.y}"



  22. # struct PointF in C\C++        (32-bit platform):
  23. #
  24. # struct PointF
  25. # {
  26. #    double x;
  27. #    double y;
  28. # };

  29. # struct PointF in Ruby:
  30. class PointF < CStruct
  31.   double:x
  32.   double:y
  33. end

  34. # create a PointF's instance
  35. # use 'block' to initialize the fields
  36. point2   = PointF.new do |st|
  37.         st.x = 10.56
  38.         st.y = 20.78
  39. end

  40. puts "sizeof(PointF) = #{PointF.size}"
  41. puts "point2.x = #{point2.x},point2.y = #{point2.y}"
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-01-14 13:44 |只看该作者
[代码] 数组成员
  1. 01 # CStruct Examples  

  2. 02 require 'cstruct'

  3. 03   

  4. 04 # example:  

  5. 05 # struct T in C\C++ (32-bit platform):   

  6. 06 #   

  7. 07 # struct T  

  8. 08 # {  

  9. 09 #    int element[8];  

  10. 10 # };   

  11. 11   

  12. 12 # struct T in Ruby:   

  13. 13 class T < CStruct  

  14. 14   int32:elements,[8]  

  15. 15 end

  16. 16   

  17. 17 # create a T's instance  

  18. 18 t_array = T.new

  19. 19   

  20. 20 (0..7).each do |i|  

  21. 21         t_array.elements[i] = i  # assign like as C language  

  22. 22 end

  23. 23   

  24. 24 # output  

  25. 25 (0..7).each {|i| puts "t_array.elements[#{i}] = #{t_array.elements[i]}" }  

  26. 26   

  27. 27   

  28. 28 # Actually,t_array.elements.class is Array. So..  

  29. 29 t_array.elements.each {|element| puts element }
复制代码
[代码] 结构体嵌套
  1. 01 # CStruct Examples  

  2. 02 require 'cstruct'

  3. 03 # example:  

  4. 04 # struct A in C\C++ (32-bit platform):   

  5. 05 # struct A{  

  6. 06 #     struct Inner  

  7. 07 #     {  

  8. 08 #       int v1;   

  9. 09 #       int v2;  

  10. 10 #     };  

  11. 11 #     Inner inner;  

  12. 12 # };  

  13. 13   

  14. 14 # struct A in Ruby:  

  15. 15 class A < CStruct  

  16. 16     class Inner < CStruct  

  17. 17         int32 :v1

  18. 18         int32 :v2

  19. 19     end

  20. 20         Inner :inner

  21. 21 end

  22. 22   

  23. 23 a = A.new

  24. 24 a.inner.v1 = 1

  25. 25 a.inner.v2 = 2

  26. 26   

  27. 27 puts a.inner.v1   

  28. 28 puts a.inner.v2
复制代码
[代码] 匿名结构体
  1. 01 # CStruct Examples  

  2. 02 require 'cstruct'

  3. 03   

  4. 04 # example:  

  5. 05 # struct Window in C\C++ (32-bit platform):   

  6. 06 #   

  7. 07 # struct Window  

  8. 08 # {  

  9. 09 #    int style;  

  10. 10 #    struct{  

  11. 11 #       int x;  

  12. 12 #       int y;  

  13. 13 #    }position; /* position is anonymous struct's instance */  

  14. 14 # };   

  15. 15   

  16. 16   

  17. 17 # struct Window in Ruby:   

  18. 18 class Window < CStruct  

  19. 19     int32:style

  20. 20     struct :position do

  21. 21         int32:x

  22. 22         int32:y         

  23. 23     end

  24. 24 end

  25. 25 # or like this (use brace):  

  26. 26 # class Window < CStruct  

  27. 27 #    int32:style  

  28. 28 #    struct (:position) {  

  29. 29 #        int32:x  

  30. 30 #        int32:y         

  31. 31 #    }  

  32. 32 # end  

  33. 33   

  34. 34 # create a Window's instance  

  35. 35 window = Window.new

  36. 36   

  37. 37 # assign like as C language  

  38. 38 window.style = 1

  39. 39 window.position.x = 10

  40. 40 window.position.y = 10

  41. 41   

  42. 42 puts "sizeof(Window) = #{Window.__size__}" # "__size__" is alias of "size"

  43. 43 puts "window.style = #{window.style},window.position.x = #{window.position.x},window.position.y = #{window.position.y}"
复制代码
[代码] 命名空间
  1. 01 # CStruct Examples  

  2. 02 require 'cstruct'

  3. 03   

  4. 04 # example:  

  5. 05 module NS1    #namespace  

  6. 06     class A < CStruct  

  7. 07         uint32:handle

  8. 08     end

  9. 09      

  10. 10     module NS2

  11. 11         class B < CStruct  

  12. 12             A:a        # directly use A  

  13. 13         end  

  14. 14     end

  15. 15   

  16. 16     class C < CStruct  

  17. 17       A     :a

  18. 18       NS2_B :b        # Meaning of the 'NS2_B' is NS2::B  

  19. 19     end         

  20. 20 end

  21. 21   

  22. 22 class D < CStruct  

  23. 23     NS1_NS2_B:b        # Meaning of the 'NS1_NS2_B' is NS1::NS2::B  

  24. 24 end         

  25. 25   

  26. 26 v = D.new

  27. 27 v.b.a.handle = 120

  28. 28 p D.__size__  

  29. 29 p v.b.a.handle
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-01-15 22:27 |只看该作者
本帖最后由 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窗口
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP