Sha256: 195473950b76a14980339965cea8119b6cff49416459d33cb93223d6f947e9fa

Contents?: true

Size: 797 Bytes

Versions: 13

Compression:

Stored size: 797 Bytes

Contents

module SlimScrooge
  class SimpleSet < Hash
    class << self
      # Creates a new set containing the given objects
      def [](*ary)
        new(ary)
      end
    end
 
    # Create a new SimpleSet containing the unique members of _arr_
    def initialize(arr = [])
      Array(arr).each {|x| self[x] = true}
    end
 
    # Add a value to the set, and return it
    def <<(value)
      self[value] = true
      self
    end

    # Invokes block once for each item in the set. Creates an array
    # containing the values returned by the block.
    def collect(&block)
      keys.collect(&block)
    end

    alias_method :to_a, :keys

    # Returns set after elements in other have been removed
    def -(other)
      SimpleSet.new(collect {|k| other[k] ? nil : k}.compact)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
slim_scrooge-1.0.14 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.12 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.11 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.10 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.9 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.7 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.5 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.4 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.3 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.2 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.1 lib/slim_scrooge/simple_set.rb
slim_scrooge-1.0.0 lib/slim_scrooge/simple_set.rb
slim_scrooge-0.1.1 lib/slim_scrooge/simple_set.rb