Class: Lafcadio::QueueHash
- Inherits:
-
Object
- Object
- Lafcadio::QueueHash
- Defined in:
- lib/lafcadio/util/QueueHash.rb
Overview
An ordered hash: Keys are ordered according to when they were inserted.
Class Method Summary collapse
-
.newFromArray(array) ⇒ Object
Creates a QueueHash with all the elements in
array
as keys, and each value initially set to be the same as the corresponding key.
Instance Method Summary collapse
- #==(otherObj) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each ⇒ Object
-
#initialize(*values) ⇒ QueueHash
constructor
Takes an even number of arguments, and sets each odd-numbered argument to correspond to the argument immediately afterward.
- #keys ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(*values) ⇒ QueueHash
Takes an even number of arguments, and sets each odd-numbered argument to correspond to the argument immediately afterward. For example:
queueHash = QueueHash.new (1, 2, 3, 4)
queueHash[1] => 2
queueHash[3] => 4
20 21 22 23 |
# File 'lib/lafcadio/util/QueueHash.rb', line 20 def initialize(*values) @pairs = [] 0.step(values.size-1, 2) { |i| @pairs << [ values[i], values[i+1] ] } end |
Class Method Details
.newFromArray(array) ⇒ Object
Creates a QueueHash with all the elements in array
as keys, and each value initially set to be the same as the corresponding key.
6 7 8 9 10 11 12 13 |
# File 'lib/lafcadio/util/QueueHash.rb', line 6 def QueueHash.newFromArray(array) valueArray = [] array.each { |elt| valueArray << elt valueArray << elt } new(*valueArray) end |
Instance Method Details
#==(otherObj) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lafcadio/util/QueueHash.rb', line 55 def ==( otherObj ) if otherObj.class == QueueHash && otherObj.size == size match = true (0...size).each { |i| match &&= keys[i] == otherObj.keys[i] && values[i] == otherObj.values[i] } match else false end end |
#[](key) ⇒ Object
37 38 39 40 41 |
# File 'lib/lafcadio/util/QueueHash.rb', line 37 def [](key) value = nil @pairs.each { |pair| value = pair[1] if pair[0] == key } value end |
#[]=(key, value) ⇒ Object
47 48 49 |
# File 'lib/lafcadio/util/QueueHash.rb', line 47 def []=(key, value) @pairs << [key, value] end |
#each ⇒ Object
51 52 53 |
# File 'lib/lafcadio/util/QueueHash.rb', line 51 def each @pairs.each { |pair| yield pair[0], pair[1] } end |
#keys ⇒ Object
25 26 27 28 29 |
# File 'lib/lafcadio/util/QueueHash.rb', line 25 def keys keys = [] @pairs.each { |pair| keys << pair[0] } keys end |
#size ⇒ Object
43 44 45 |
# File 'lib/lafcadio/util/QueueHash.rb', line 43 def size @pairs.size end |
#values ⇒ Object
31 32 33 34 35 |
# File 'lib/lafcadio/util/QueueHash.rb', line 31 def values values = [] @pairs.each { |pair| values << pair[1] } values end |