博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby 仿 C 结构体:CStruct 的一些例子
阅读量:5269 次
发布时间:2019-06-14

本文共 2860 字,大约阅读时间需要 9 分钟。

1. [代码]最简单的例子     
# 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}"
2. [代码]数组成员     

# CStruct Examples
require 'cstruct'
 
# example:
# struct T in C\C++ (32-bit platform): 

# struct T
# {

#    int element[8];
# }; 
 
# struct T in Ruby: 
class T < CStruct
  int32:elements,[8]
end
 
# create a T's instance
t_array = T.new
 
(0..7).each do |i|
        t_array.elements[i] = i  # assign like as C language
endhttp://www.enterdesk.com/special/huangguantp/
 
# output
(0..7).each {|i| puts "t_array.elements[#{i}] = #{t_array.elements[i]}" }
 
 
# Actually,t_array.elements.class is Array. So..
t_array.elements.each {|element| puts element }
3. [代码]结构体嵌套     
# CStruct Examples
require 'cstruct'
# example:
# struct A in C\C++ (32-bit platform): 
# struct A{

#     struct Inner
#     {

#       int v1; 
#       int v2;
#     };
#     Inner inner;
# };
 
# struct A in Ruby:
class A < CStruct
    class Inner < CStruct
        int32 :v1
        int32 :v2
    end
        Inner :inner
end
 
a = A.new
a.inner.v1 = 1
a.inner.v2 = 2
 
puts a.inner.v1 
puts a.inner.v2 
4. [代码]匿名结构体     

# 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}"
5. [代码]命名空间     

# CStruct Examples
require 'cstruct'
 
# example:
module NS1    #namespace
    class A < CStruct
        uint32:handle
    end
   
    module NS2
        class B < CStruct
            A:a        # directly use A
        end
    end
 
    class C < CStruct
      A     :a
      NS2_B :b        # Meaning of the 'NS2_B' is NS2::B
    end       
end
 
class D < CStruct
    NS1_NS2_B:b        # Meaning of the 'NS1_NS2_B' is NS1::NS2::B
end       
 
v = D.new
v.b.a.handle = 120
p D.__size__
p v.b.a.handle

转载于:https://www.cnblogs.com/xkzy/p/3958056.html

你可能感兴趣的文章
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
Linux发行版的排行
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
daydayup2 codeforces143C
查看>>
ANT打包J2EE项目war包
查看>>
UESTC-我要长高 DP优化
查看>>
java选择文件时提供图像缩略图[转]
查看>>
当DIV内出现滚动条,fixed实效怎么办?
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
string与stringbuilder的区别
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
iOS8统一的系统提示控件——UIAlertController
查看>>
PAT甲级——1101 Quick Sort (快速排序)
查看>>
python创建进程的两种方式
查看>>
1.2 基础知识——关于猪皮(GP,Generic Practice)
查看>>