Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图连接两个子类数组。
但它返回一个数组类而不是 MyArray。
class MyArray < Array end foo = MyArray.new bar = MyArray.new p foo.class #=> MyArray p (foo + bar).class #=> Array
如何连接 MyArray 类?
在 MyArray 类中定义方法并使用super. 你也可以只使用 alias_method :+, :concat
super
def concat(some_array) super end p foo.concat(bar).class #=> MyArray