Class: Comodule::UniArray

Inherits:
Array
  • Object
show all
Defined in:
lib/comodule/uni_array.rb

Overview

.<<()で追加されるメンバが重複する場合は無視するArrayのサブクラス。

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_sizeObject

Returns the value of attribute max_size.



4
5
6
# File 'lib/comodule/uni_array.rb', line 4

def max_size
  @max_size
end

Instance Method Details

#<<(arg) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/comodule/uni_array.rb', line 6

def <<(arg)
  if member?(arg)
    return self
  end

  super

  # max_sizeに到達したら、先頭を切り詰めて返す。
  if max_size && size > max_size
    replace self[-max_size..-1]
  end

  self
end