[Ruby]Struct 结构体的使用
来源:互联网 发布:安卓软件推荐 编辑:程序博客网 时间:2024/06/03 03:56
Struct 结构体的使用
结构体类。由Struct.new生成该类的子类。在子类中使用new方法就可以生成构造体。构造体子类中定义了构造体成员的访问方法
n = Struct.new("N", :name, :age)obj = n.new("hello",6)=》 #<struct Struct::N name="hello", age=6>obj.class=> Struct::N obj.name=> "hello"obj.age=> 6
如果看不懂可以分解开来
module M class N attr_accessor :name ,:age def initialize(name,age) @name = name @age = age end endendfred = M::N.new(“hello”,6)fred.class =>M::Nfred.name =>"hello"fred.age =>6
只是将Struct换成了M
在结构体一般是用来存储数据的类型的一种写法,而ruby给我们提供了一种非常便捷的写法。这也是ruby的魅力之处吧!
上面的只是便于记忆,其实本质还是不同的,可以参考一下了解具体的特性
ruby查考手册struct
0 0
- [Ruby]Struct 结构体的使用
- struct结构体使用
- Struct结构体的使用学习
- C++ 结构体struct的使用
- struct utname结构体使用
- struct hostent结构体使用
- c++ 中 extern 对struct 结构体的使用
- struct passwd 结构体的相关使用函数
- C/C++中结构体struct 的使用
- swift的struct结构体类型介绍使用
- struct 结构体的创建
- struct 结构体的大小
- 结构体struct的sizeof
- C#的Struct结构体
- struct结构体的大小
- 在object 中使用struct 结构体
- Matlab 结构体(struct)使用
- 结构体struct基本使用笔记
- 商务旅行
- PTA 5-2 然后是几点
- Redis 客户端连接
- Markdown 语法指南
- 文字十六进制转换
- [Ruby]Struct 结构体的使用
- webworker 探秘
- Dungeon Master<poj2251>
- Shiro简单使用
- 华为OJ 初级:整形数组合并
- PHP中获取当前页面的完整URL
- Redis 性能测试
- 矢量表示及其计算
- 7/28 CodeForces Div2