Class: KXI::Collections::HashCollection::HashEnumerator

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/kxi/collections/hash_collection.rb

Overview

Enumerates hash as key-value pairs represented by the KeyValuePair class

Defined Under Namespace

Classes: KeyValuePair

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashEnumerator

Parameters:

  • hash (Hash)

    Hash for enumeration



45
46
47
48
49
# File 'lib/kxi/collections/hash_collection.rb', line 45

def initialize(hash)
	@hash = hash
	@keys = hash.keys
	@current = 0
end

Instance Method Details

#currentObject

Returns current item

Returns:

  • (Object)

    Current item

Raises:



69
70
71
72
# File 'lib/kxi/collections/hash_collection.rb', line 69

def current
	k = @keys[@current]
	KeyValuePair.new(k, @hash[k])
end

#nextBool

Advances enumerator to next item

Returns:

  • (Bool)

    True if item is available; false otherwise

Raises:



61
62
63
64
# File 'lib/kxi/collections/hash_collection.rb', line 61

def next
	@current += 1
	return @keys.length > @current
end

#rewindBool

Selects first item in collection

Returns:

  • (Bool)

    True if collection contains elements; otherwise false



53
54
55
56
# File 'lib/kxi/collections/hash_collection.rb', line 53

def rewind
	@current = 0
	return @keys.length > 0
end