作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想把所有这些 ruby 款待给我的类(class)(处理底层数据结构)。 include Enumarable
只完成部分工作。什么是最好的方法?
class A
def initialize
@data = 10.times.map { |e| e }
end
include Enumerable
def each(&block)
@data.each(&block)
end
end
#works great
A.new.each {}
A.new.select {|e| e > 3}
A.new.reject {|e| e > 3}
A.new.partition {|e| e > 3}
A.new.count
# those will fail
A.new.size
A.new.length
A.new.select! {|e| e > 3}
最佳答案
size
、length
和所有就地改变集合的方法,如 select!
,都不是 Enumerable 的一部分
。
如果你想要它们,你必须自己实现它们。
关于ruby - 向类添加可枚举功能等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743389/
我是一名优秀的程序员,十分优秀!