Class: QueueHash
- Inherits:
-
Array
- Object
- Array
- QueueHash
- Defined in:
- lib/queuehash.rb
Constant Summary collapse
- Version =
'0.1.0'
Class Method Summary collapse
-
.new_from_array(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
- #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
22 23 24 25 26 |
# File 'lib/queuehash.rb', line 22 def initialize(*values) @pairs = [] 0.step(values.size-1, 2) { |i| @pairs << [ values[i], values[i+1] ] } super( @pairs ) end |
Class Method Details
.new_from_array(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.
13 14 15 |
# File 'lib/queuehash.rb', line 13 def self.new_from_array(array) new( *( ( array.map { |elt| [ elt, elt ] } ).flatten ) ) end |
Instance Method Details
#==(otherObj) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/queuehash.rb', line 28 def ==( otherObj ) if otherObj.class == QueueHash && otherObj.size == size ( 0..size ).all? { |i| keys[i] == otherObj.keys[i] && values[i] == otherObj.values[i] } else false end end |
#[](key) ⇒ Object
38 39 40 |
# File 'lib/queuehash.rb', line 38 def [](key) ( pair = @pairs.find { |pair| pair[0] == key } ) ? pair.last : nil end |
#[]=(key, value) ⇒ Object
42 |
# File 'lib/queuehash.rb', line 42 def []=(key, value); @pairs << [key, value]; end |
#each ⇒ Object
44 |
# File 'lib/queuehash.rb', line 44 def each; @pairs.each { |pair| yield pair[0], pair[1] }; end |
#keys ⇒ Object
46 |
# File 'lib/queuehash.rb', line 46 def keys; @pairs.map { |pair| pair[0] }; end |
#values ⇒ Object
48 |
# File 'lib/queuehash.rb', line 48 def values; @pairs.map { |pair| pair[1] }; end |