Class: Gather::UniqueArray

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

Overview

copied from StackOverflow

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ UniqueArray

Returns a new instance of UniqueArray.



6
7
8
9
10
11
12
# File 'lib/gather.rb', line 6

def initialize(*args)
  if args.size == 1 and args[0].is_a? Array then
    super(args[0].uniq)
  else
    super(*args)
  end
end

Instance Method Details

#<<(v) ⇒ Object



18
19
20
# File 'lib/gather.rb', line 18

def <<(v)
  super(v) unless include?(v)
end

#[]=(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gather.rb', line 22

def []=(*args)
  # note: could just call super(*args) then uniq!, but this is faster

  # there are three different versions of this call:
  # 1. start, length, value
  # 2. index, value
  # 3. range, value
  # We just need to get the value
  v = case args.size
    when 3 then args[2]
    when 2 then args[1]
    else nil
  end

  super(*args) if v.nil? or not include?(v)
end

#insert(i, v) ⇒ Object



14
15
16
# File 'lib/gather.rb', line 14

def insert(i, v)
  super(i, v) unless include?(v)
end