Class: SlimScrooge::SimpleSet

Inherits:
Hash
  • Object
show all
Defined in:
lib/slim_scrooge/simple_set.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#c_update, #update

Constructor Details

#initialize(arr = []) ⇒ SimpleSet

Create a new SimpleSet containing the unique members of arr



11
12
13
# File 'lib/slim_scrooge/simple_set.rb', line 11

def initialize(arr = [])
  Array(arr).each {|x| self[x] = true}
end

Class Method Details

.[](*ary) ⇒ Object

Creates a new set containing the given objects



5
6
7
# File 'lib/slim_scrooge/simple_set.rb', line 5

def [](*ary)
  new(ary)
end

Instance Method Details

#-(other) ⇒ Object

Returns set after elements in other have been removed



30
31
32
# File 'lib/slim_scrooge/simple_set.rb', line 30

def -(other)
  SimpleSet.new(collect {|k| other[k] ? nil : k}.compact)
end

#<<(value) ⇒ Object

Add a value to the set, and return it



16
17
18
19
# File 'lib/slim_scrooge/simple_set.rb', line 16

def <<(value)
  self[value] = true
  self
end

#collect(&block) ⇒ Object

Invokes block once for each item in the set. Creates an array containing the values returned by the block.



23
24
25
# File 'lib/slim_scrooge/simple_set.rb', line 23

def collect(&block)
  keys.collect(&block)
end