Class: Cape::HashList Private
- Inherits:
-
Array
- Object
- Array
- Cape::HashList
- 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
-
#==(other) ⇒ true, false
private
Compares a HashList to another object.
-
#[](key) ⇒ Object?
private
Retrieves a value from the HashList.
-
#[]=(key, value) ⇒ HashList
private
Sets a value in the HashList.
-
#initialize(*arguments) ⇒ HashList
constructor
private
Constructs a new HashList using the specified arguments.
-
#inspect ⇒ String
private
Provides a string representation of the HashList.
-
#to_hash ⇒ Hash
private
Converts the HashList to a Hash.
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.
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.
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.
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.
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 |
#inspect ⇒ String
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.
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_hash ⇒ Hash
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.
76 77 78 |
# File 'lib/cape/hash_list.rb', line 76 def to_hash ::Hash[to_a] end |