Class: NSMutableSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/project/motion-set.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*ary) ⇒ Object



14
15
16
# File 'lib/project/motion-set.rb', line 14

def self.[](*ary)
  new.tap { |set| ary.each { |item| set.add item } }
end

.new(enum = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/project/motion-set.rb', line 6

def self.new(enum=nil)
  if enum
    enum.to_set
  else
    alloc.init
  end
end

Instance Method Details

#^(enum) ⇒ Object



36
37
38
# File 'lib/project/motion-set.rb', line 36

def ^(enum)
  ((self | enum) - (self & enum))
end

#add(o) ⇒ Object Also known as: <<

Why can’t you just: alias_method :add, :‘addObject:’



33
# File 'lib/project/motion-set.rb', line 33

def add(o); addObject o end

#add?(o) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/project/motion-set.rb', line 40

def add?(o)
  (include? o) ? nil : (add o)
end

#classify(&block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/project/motion-set.rb', line 44

def classify(&block)
  return to_enum(__method__) unless block_given?
  reduce({}) do |hash, item|
    classifier = block.call item
    hash[classifier] ||= Set[]
    hash[classifier].add item
    hash
  end
end

#collect!(&block) ⇒ Object Also known as: map!



56
57
58
59
# File 'lib/project/motion-set.rb', line 56

def collect!(&block)
  return to_enum(__method__) unless block_given?
  replace collect(&block)
end

#delete(o) ⇒ Object

Why can’t you just: alias_method :delete, :‘removeObject:’



63
# File 'lib/project/motion-set.rb', line 63

def delete(o); removeObject o end

#delete?(o) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/project/motion-set.rb', line 65

def delete?(o)
  (include? o) ? (delete o) : nil
end

#delete_if(&block) ⇒ Object



75
76
77
78
79
# File 'lib/project/motion-set.rb', line 75

def delete_if(&block)
  return to_enum(__method__) unless block_given?
  each { |item| delete item if block.call item }
  self
end

#difference(enum) ⇒ Object Also known as: -



29
# File 'lib/project/motion-set.rb', line 29

def difference(enum); send_to_copy(:'minusSet:', enum) end

#empty?Boolean

Returns:

  • (Boolean)


85
# File 'lib/project/motion-set.rb', line 85

def empty?; count == 0 end

#flattenObject



87
88
89
# File 'lib/project/motion-set.rb', line 87

def flatten
  self.to_enum(:flat_each).to_set
end

#flatten!Object



91
92
93
94
95
96
# File 'lib/project/motion-set.rb', line 91

def flatten!
  copy = self.to_set
  # Not sure why self.to_enum does not work in this case:
  replace copy.to_enum :flat_each
  (copy == self) ? nil : self
end

#inspectObject



18
19
20
# File 'lib/project/motion-set.rb', line 18

def inspect
  "#<Set: {#{self.to_a.join ', '}}>"
end

#intersection(enum) ⇒ Object Also known as: &



22
# File 'lib/project/motion-set.rb', line 22

def intersection(enum); send_to_copy(:'intersectSet', enum) end

#keep_if(&block) ⇒ Object



69
70
71
72
73
# File 'lib/project/motion-set.rb', line 69

def keep_if(&block)
  return to_enum(__method__) unless block_given?
  each { |item| delete item unless block.call item }
  self
end

#lengthObject Also known as: size

Why can’t you just: alias_method :length, :count



82
# File 'lib/project/motion-set.rb', line 82

def length; count end

#merge(enum) ⇒ Object



101
102
103
104
# File 'lib/project/motion-set.rb', line 101

def merge(enum)
  enum.each { |item| add item }
  self
end

#proper_subset?(set) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/project/motion-set.rb', line 124

def proper_subset?(set)
  (subset? set) && (self != set)
end

#proper_superset?(set) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/project/motion-set.rb', line 128

def proper_superset?(set)
  (superset? set) && (self != set)
end

#reject!(&block) ⇒ Object



132
133
134
135
# File 'lib/project/motion-set.rb', line 132

def reject!(&block)
  return to_enum(__method__) unless block_given?
  each_returning_self_or_nil { |item| delete item if block.call item }
end

#replace(enum) ⇒ Object



106
107
108
109
# File 'lib/project/motion-set.rb', line 106

def replace(enum)
  clear
  merge(enum)
end

#select!(&block) ⇒ Object



137
138
139
140
# File 'lib/project/motion-set.rb', line 137

def select!(&block)
  return to_enum(__method__) unless block_given?
  each_returning_self_or_nil { |item| delete item unless block.call item }
end

#subset?(set) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/project/motion-set.rb', line 116

def subset?(set)
  enforce_set(set) && isSubsetOfSet(set)
end

#subtract(enum) ⇒ Object



111
112
113
114
# File 'lib/project/motion-set.rb', line 111

def subtract(enum)
  enum.each { |item| delete item }
  self
end

#superset?(set) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/project/motion-set.rb', line 120

def superset?(set)
  enforce_set(set) && set.isSubsetOfSet(self)
end

#union(enum) ⇒ Object Also known as: |, +



25
# File 'lib/project/motion-set.rb', line 25

def union(enum); send_to_copy(:'unionSet:', enum) end