Class: Set

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/stdlib/set.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#to_json, #to_set

Constructor Details

#initialize(enum = nil, &block) ⇒ Set

Returns a new instance of Set

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'opal/stdlib/set.rb', line 8

def initialize(enum = nil, &block)
  @hash = Hash.new

  return if enum.nil?
  raise ArgumentError, 'value must be enumerable' unless Enumerable === enum

  if block
    enum.each { |item| add block.call(item) }
  else
    merge(enum)
  end
end

Class Method Details

.[](*ary) ⇒ Object



4
5
6
# File 'opal/stdlib/set.rb', line 4

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

Instance Method Details

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



26
27
28
29
30
31
32
# File 'opal/stdlib/set.rb', line 26

def -(enum)
  unless enum.respond_to? :each
    raise ArgumentError, "value must be enumerable"
  end

  dup.subtract(enum)
end

#==(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'opal/stdlib/set.rb', line 39

def ==(other)
  if self.equal?(other)
    true
  elsif other.instance_of?(self.class)
    @hash == other.instance_variable_get(:@hash)
  elsif other.is_a?(Set) && self.size == other.size
    other.all? { |o| @hash.include?(o) }
  else
    false
  end
end

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



51
52
53
54
# File 'opal/stdlib/set.rb', line 51

def add(o)
  @hash[o] = true
  self
end

#add?(o) ⇒ Boolean

Returns:



97
98
99
100
101
102
103
# File 'opal/stdlib/set.rb', line 97

def add?(o)
  if include?(o)
    nil
  else
    add(o)
  end
end

#classify(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'opal/stdlib/set.rb', line 57

def classify(&block)
  return enum_for(:classify) unless block_given?

  result = Hash.new { |h, k| h[k] = self.class.new }

  each { |item| result[yield(item)].add item }

  result
end

#clearObject



119
120
121
122
# File 'opal/stdlib/set.rb', line 119

def clear
  @hash.clear
  self
end

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



67
68
69
70
71
72
# File 'opal/stdlib/set.rb', line 67

def collect!(&block)
  return enum_for(:collect!) unless block_given?
  result = self.class.new
  each { |item| result << yield(item) }
  replace result
end

#delete(o) ⇒ Object



75
76
77
78
# File 'opal/stdlib/set.rb', line 75

def delete(o)
  @hash.delete(o)
  self
end

#delete?(o) ⇒ Boolean

Returns:



80
81
82
83
84
85
86
87
# File 'opal/stdlib/set.rb', line 80

def delete?(o)
  if include?(o)
    delete(o)
    self
  else
    nil
  end
end

#delete_ifObject



89
90
91
92
93
94
95
# File 'opal/stdlib/set.rb', line 89

def delete_if
  block_given? or return enum_for(__method__)
  # @hash.delete_if should be faster, but using it breaks the order
  # of enumeration in subclasses.
  select { |o| yield o }.each { |o| @hash.delete(o) }
  self
end

#dupObject



21
22
23
24
# File 'opal/stdlib/set.rb', line 21

def dup
  result = self.class.new
  result.merge(self)
end

#each(&block) ⇒ Object



105
106
107
108
109
# File 'opal/stdlib/set.rb', line 105

def each(&block)
  return enum_for :each unless block_given?
  @hash.each_key(&block)
  self
end

#empty?Boolean

Returns:



111
112
113
# File 'opal/stdlib/set.rb', line 111

def empty?
  @hash.empty?
end

#eql?(other) ⇒ Boolean

Returns:



115
116
117
# File 'opal/stdlib/set.rb', line 115

def eql?(other)
  @hash.eql?(other.instance_eval { @hash })
end

#include?(o) ⇒ Boolean Also known as: member?

Returns:



124
125
126
# File 'opal/stdlib/set.rb', line 124

def include?(o)
  @hash.include?(o)
end

#inspectObject



35
36
37
# File 'opal/stdlib/set.rb', line 35

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

#merge(enum) ⇒ Object



129
130
131
132
# File 'opal/stdlib/set.rb', line 129

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

#replace(enum) ⇒ Object



134
135
136
137
138
139
# File 'opal/stdlib/set.rb', line 134

def replace(enum)
  clear
  merge(enum)

  self
end

#sizeObject Also known as: length



141
142
143
# File 'opal/stdlib/set.rb', line 141

def size
  @hash.size
end

#subtract(enum) ⇒ Object



146
147
148
149
# File 'opal/stdlib/set.rb', line 146

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

#to_aObject



151
152
153
# File 'opal/stdlib/set.rb', line 151

def to_a
  @hash.keys
end