Class: Cape::HashList Private

Inherits:
Array
  • Object
show all
Defined in:
lib/cape/hash_list.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A HashList is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.

This class exists because in Ruby v1.8.7 and earlier, Hash did not preserve the insertion order of keys.

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ HashList

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Constructs a new HashList using the specified arguments.

Parameters:

  • arguments (Hash)

    attribute values



17
18
19
# File 'lib/cape/hash_list.rb', line 17

def initialize(*arguments)
  super Hash[*arguments].to_a
end

Instance Method Details

#==(other) ⇒ true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compares a HashList to another object.

Parameters:

  • other (Object)

    another object

Returns:

  • (true)

    the HashList has the same number of keys as other, and each key-value pair is equal (according to Object#==)

  • (false)

    the HashList is not equal to other



28
29
30
# File 'lib/cape/hash_list.rb', line 28

def ==(other)
  other.is_a?(::Hash) ? (other == to_hash) : super(other)
end

#[](key) ⇒ Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves a value from the HashList.

Parameters:

  • key (Object)

    a key

Returns:

  • (Object)

    the value for key

  • (nil)

    if there is no value for key



38
39
40
41
42
43
# File 'lib/cape/hash_list.rb', line 38

def [](key)
  entry = find do |pair|
    Array(pair).first == key
  end
  entry ? Array(entry).last : nil
end

#[]=(key, value) ⇒ HashList

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets a value in the HashList.

Parameters:

  • key (Object)

    a key

  • value (Object)

    a value for key

Returns:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cape/hash_list.rb', line 51

def []=(key, value)
  index = find_index do |pair|
    Array(pair).first == key
  end
  if index
    super(index, [key, value])
  else
    self << [key, value]
  end
  self
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides a string representation of the HashList.

Returns:

  • (String)

    a string representation of the HashList



66
67
68
69
70
71
# File 'lib/cape/hash_list.rb', line 66

def inspect
  entries = collect do |pair|
    Array(pair).collect(&:inspect).join '=>'
  end
  "{#{entries.join ', '}}"
end

#to_hashHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the HashList to a Hash.

Returns:

  • (Hash)

    a hash



76
77
78
# File 'lib/cape/hash_list.rb', line 76

def to_hash
  ::Hash[to_a]
end