我想编写一个模块,它提供了有关数组实例变量的活动记录功能。其使用的活动记录像数组实例变量的功能
例子是
x = Container.new
x.include(ContainerModule)
x.elements << Element.new
x.elements.find id
module ContainerModule
def initialize(*args)
@elements = []
class << @elements
def <<(element)
#do something with the Container...
super(element)
end
def find(id)
#find an element using the Container's id
self
#=> #<Array..> but I need #<Container..>
end
end
super(*args)
end
end
的问题是,我需要这些方法中的容器对象。对self的任何引用都会返回Array,而不是Container对象。
有没有办法做到这一点?
谢谢!
哈希真的不是一个选项。它需要像一个数组那样工作,因为代码库使用它。 – stellard 2010-05-10 23:03:01
我可能会误解你需要什么,但是不能把hash_object.values()传递给需要用数组处理的事情吗? – corprew 2010-05-11 16:44:43