I need to override the operator << for one single attribute in my class.
Basically, what I want is allow to push only unique integers to my array attribute.
Here's what I have:
class Player
attr_accessor :moves
def initialize
@moves = []
end
def moves<<(value)
raise Exception if @moves.include?(value)
@moves.push(value)
end
end
Unfortunately, this code doesn't work.
How can I improve that or maybe there are better ways to implement such functionality?