aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/extend/set.rb
blob: e67a05792cd82b734788e8401c112c3552a9834b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'set'

class ComparableSet < Set
  def add new
    # smileys only
    return super new unless new.respond_to? :>

    objs = find_all { |o| o.class == new.class }
    objs.each do |o|
      return self if o > new
      delete o
    end
    super new
  end

  alias_method :<<, :add

  # Set#merge bypasses enumerating the set's contents,
  # so the subclassed #add would never be called
  def merge enum
    enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable"
    enum.each { |o| add(o) }
    self
  end
end