Tetsu
Tetsu

Reputation: 213

Apply + operator to subclassing array

I tried to concatenate two subclassing array.

But it returns a Array class not MyArray.

class MyArray < Array
end

foo = MyArray.new
bar = MyArray.new
p foo.class #=> MyArray
p (foo + bar).class #=> Array

How can I concatenate MyArray classes?

Upvotes: 0

Views: 60

Answers (1)

dasnixon
dasnixon

Reputation: 998

Define the method in your MyArray class and use super. You could also just alias_method :+, :concat

def concat(some_array)
  super
end

p foo.concat(bar).class #=> MyArray

Upvotes: 2

Related Questions