Class: Cassandra::OrderedHashInt
- Inherits:
-
Hash
- Object
- Hash
- Cassandra::OrderedHashInt
- Defined in:
- lib/cassandra/ordered_hash.rb
Overview
:nodoc:
Direct Known Subclasses
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) []=(key, value)
- - (Object) clear
- - (Object) delete(key)
- - (Object) delete_if
- - (Object) each (also: #each_pair)
- - (Object) each_key
- - (Object) each_value
-
- (OrderedHashInt) initialize(*args, &block)
constructor
A new instance of OrderedHashInt.
- - (Object) initialize_copy(other)
- - (Object) keys
- - (Object) merge(other_hash)
- - (Object) merge!(other_hash)
- - (Object) reject(&block)
- - (Object) reject!
-
- (Object) replace(other)
When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
- - (Object) reverse
- - (Object) shift
- - (Object) to_a
- - (Object) to_hash
- - (Object) values
Constructor Details
- (OrderedHashInt) initialize(*args, &block)
A new instance of OrderedHashInt
4 5 6 7 |
# File 'lib/cassandra/ordered_hash.rb', line 4 def initialize(*args, &block) super @keys = [] end |
Class Method Details
+ (Object) [](*args)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cassandra/ordered_hash.rb', line 9 def self.[](*args) ordered_hash = new if (args.length == 1 && args.first.is_a?(Array)) args.first.each do |key_value_pair| next unless (key_value_pair.is_a?(Array)) ordered_hash[key_value_pair[0]] = key_value_pair[1] end return ordered_hash end unless (args.size % 2 == 0) raise ArgumentError.new("odd number of arguments for Hash") end args.each_with_index do |val, ind| next if (ind % 2 != 0) ordered_hash[val] = args[ind + 1] end ordered_hash end |
Instance Method Details
- (Object) []=(key, value)
39 40 41 42 |
# File 'lib/cassandra/ordered_hash.rb', line 39 def []=(key, value) @keys << key if !has_key?(key) super end |
- (Object) clear
98 99 100 101 102 |
# File 'lib/cassandra/ordered_hash.rb', line 98 def clear super @keys.clear self end |
- (Object) delete(key)
44 45 46 47 48 49 50 |
# File 'lib/cassandra/ordered_hash.rb', line 44 def delete(key) if has_key? key index = @keys.index(key) @keys.delete_at index end super end |
- (Object) delete_if
52 53 54 55 56 |
# File 'lib/cassandra/ordered_hash.rb', line 52 def delete_if super sync_keys! self end |
- (Object) each Also known as: each_pair
92 93 94 |
# File 'lib/cassandra/ordered_hash.rb', line 92 def each @keys.each {|key| yield [key, self[key]]} end |
- (Object) each_key
84 85 86 |
# File 'lib/cassandra/ordered_hash.rb', line 84 def each_key @keys.each { |key| yield key } end |
- (Object) each_value
88 89 90 |
# File 'lib/cassandra/ordered_hash.rb', line 88 def each_value @keys.each { |key| yield self[key]} end |
- (Object) initialize_copy(other)
33 34 35 36 37 |
# File 'lib/cassandra/ordered_hash.rb', line 33 def initialize_copy(other) super # make a deep copy of keys @keys = other.keys end |
- (Object) keys
68 69 70 |
# File 'lib/cassandra/ordered_hash.rb', line 68 def keys @keys.dup end |
- (Object) merge(other_hash)
115 116 117 |
# File 'lib/cassandra/ordered_hash.rb', line 115 def merge(other_hash) dup.merge!(other_hash) end |
- (Object) merge!(other_hash)
110 111 112 113 |
# File 'lib/cassandra/ordered_hash.rb', line 110 def merge!(other_hash) other_hash.each {|k,v| self[k] = v } self end |
- (Object) reject(&block)
64 65 66 |
# File 'lib/cassandra/ordered_hash.rb', line 64 def reject(&block) dup.reject!(&block) end |
- (Object) reject!
58 59 60 61 62 |
# File 'lib/cassandra/ordered_hash.rb', line 58 def reject! super sync_keys! self end |
- (Object) replace(other)
When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
120 121 122 123 124 |
# File 'lib/cassandra/ordered_hash.rb', line 120 def replace(other) super @keys = other.keys self end |
- (Object) reverse
126 127 128 |
# File 'lib/cassandra/ordered_hash.rb', line 126 def reverse OrderedHashInt[self.to_a.reverse] end |
- (Object) shift
104 105 106 107 108 |
# File 'lib/cassandra/ordered_hash.rb', line 104 def shift k = @keys.first v = delete(k) [k, v] end |
- (Object) to_a
80 81 82 |
# File 'lib/cassandra/ordered_hash.rb', line 80 def to_a @keys.map { |key| [ key, self[key] ] } end |
- (Object) to_hash
76 77 78 |
# File 'lib/cassandra/ordered_hash.rb', line 76 def to_hash self end |
- (Object) values
72 73 74 |
# File 'lib/cassandra/ordered_hash.rb', line 72 def values @keys.collect { |key| self[key] } end |