2008-01-27
为BitStruct添加list类型
关键字: bitstruct list类型 扩展class BitStruct
def self.list(name,element)
module_eval <<-HERE
rest :body
def #{name.to_s}=(elist)
e=Object.const_get(:#{element})
if elist.respond_to? :each and elist.respond_to? :[] and elist[0].is_a? e
elist.each{ |value| self.body+=value}
elsif elist.is_a? e
self.body=elist
else
raise ArgumentError,elist.to_s<<" is not a #{element}",caller
end
end
def #{name.to_s}
result=[]
e=Object.const_get(:#{element})
size=e.new.size
num=self.body.size/size-1
for i in 0..num
result[i]=e.new(self.body[i*size,size])
end
result
end
HERE
end
end
使用的话:
class A <BitStruct
unsigned :id,32
end
class B<BitStruct
list :aList,A
end
alist=[]
for i in 0..10
a=A.new
a.id=i
alist.unshift a
end
b=B.new
b.aList=alist
b.aList.each{|e| puts e.id}
- 15:50
- 浏览 (395)
- 评论 (0)
- 分类: ruby & rails
- 相关推荐
发表评论
- 浏览: 145397 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
最新评论
-
最近的学习和工作
楼主住在棠下。学的一些技术我都没有做过 不过ruby 还是会一点点的
-- by penghao122 -
PL/SQL学习笔记(五)
ELSEIF不对,应该是ELSIF
-- by gmizr -
oracle table-lock的5种 ...
select for update 应该是row share mode的锁, 也 ...
-- by xiaoxiao1984 -
oracle table-lock的5种 ...
如果允许别的session查询或用select for update锁定记录,不 ...
-- by xiaoxiao1984 -
Hadoop分布式文件系统:架 ...
beijing.josh 写道dennis_zane 写道sunhengxin ...
-- by dogstar






评论排行榜