class UniqEnumerator

Enumerator object which removes duplicate entries. See also Object#enum_uniq and Enumerable#each_uniq

Public Class Methods

new(obj, enum_with, args, key = nil) click to toggle source

Creates the enumerator on obj using the method enum_with to enumerate. The method will be called with the arguments in args.

If key is given, it is a proc object which should return the key on which we base ourselves to compare two objects. If it is not given, UniqEnumerator uses the object itself

See also Object#enum_uniq and Enumerable#each_uniq

Calls superclass method
# File lib/utilrb/enumerable/uniq.rb, line 16
def initialize(obj, enum_with, args, key = nil)
    super(obj, enum_with, *args)
    @key = key
    @result = Hash.new
end

Public Instance Methods

each() { |v| ... } click to toggle source
Calls superclass method
# File lib/utilrb/enumerable/uniq.rb, line 22
def each
    if block_given?
        @result.clear
        result = @result
        super() do |v|
            k = @key ? @key.call(v) : v

            if !result.has_key?(k)
                result[k] = v
                yield(v)
            end
        end

        result.values
    else
        self
    end
end