The order of iteration over hashes in Ruby 1.8 is undefined. For example,
you do not know the order in which keys
will return keys, or
each
yield pairs. ActiveSupport::OrderedHash
implements a hash that preserves insertion order, as in Ruby 1.9:
oh = ActiveSupport::OrderedHash.new
oh[:a] = 1
oh[:b] = 2
oh.keys
ActiveSupport::OrderedHash
is namespaced to prevent conflicts
with other implementations.
Methods
- #
-
- C
-
- D
-
- E
-
- I
-
- K
-
- M
-
- N
-
- R
-
- S
-
- T
-
- V
-
Class Public methods
Source:
show
| on GitHub
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
In MRI the Hash class is core and written in C.
In particular, methods are programmed with explicit C function calls and
polymorphism is not honored.
For example, []= is crucial in this implementation to maintain the @keys
array but hash.c invokes rb_hash_aset() originally. This prevents method
reuse through inheritance and forces us to reimplement stuff.
For instance, we cannot use the inherited merge! because albeit the
algorithm itself would work, our []= is not being called at all by the C
code.
Source:
show
| on GitHub
def initialize(*args, &block)
super
@keys = []
end
Instance Public methods
Source:
show
| on GitHub
def []=(key, value)
@keys << key unless has_key?(key)
super
end
Source:
show
| on GitHub
def delete(key)
if has_key? key
index = @keys.index(key)
@keys.delete_at index
end
super
end
Source:
show
| on GitHub
def each
return to_enum(:each) unless block_given?
@keys.each {|key| yield [key, self[key]]}
self
end
Source:
show
| on GitHub
def each_key
return to_enum(:each_key) unless block_given?
@keys.each { |key| yield key }
self
end
Source:
show
| on GitHub
def each_pair
return to_enum(:each_pair) unless block_given?
@keys.each {|key| yield key, self[key]}
self
end
Source:
show
| on GitHub
def each_value
return to_enum(:each_value) unless block_given?
@keys.each { |key| yield self[key]}
self
end
Source:
show
| on GitHub
def encode_with(coder)
coder.represent_seq '!omap', map { |k,v| { k => v } }
end
Returns true to make sure that this hash is extractable via
Array#extract_options!
Source:
show
| on GitHub
def initialize_copy(other)
super
@keys = other.keys
end
Source:
show
| on GitHub
def inspect
"#<OrderedHash #{super}>"
end
Source:
show
| on GitHub
def invert
OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
end
merge(other_hash, &block)
Source:
show
| on GitHub
def merge(other_hash, &block)
dup.merge!(other_hash, &block)
end
Source:
show
| on GitHub
def merge!(other_hash)
if block_given?
other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
else
other_hash.each { |k, v| self[k] = v }
end
self
end
nested_under_indifferent_access()
Source:
show
| on GitHub
def nested_under_indifferent_access
self
end
Source:
show
| on GitHub
def reject(&block)
dup.reject!(&block)
end
When replacing with another hash, the initial order of our keys must come
from the other hash -ordered or not.
Source:
show
| on GitHub
def replace(other)
super
@keys = other.keys
self
end
Source:
show
| on GitHub
def shift
k = @keys.first
v = delete(k)
[k, v]
end
Source:
show
| on GitHub
def sync_keys!
@keys.delete_if {|k| !has_key?(k)}
end
Source:
show
| on GitHub
def to_a
@keys.map { |key| [ key, self[key] ] }
end
Source:
show
| on GitHub
def to_yaml(opts = {})
if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
return super
end
YAML.quick_emit(self, opts) do |out|
out.seq(taguri) do |seq|
each do |k, v|
seq.add(k => v)
end
end
end
end
Source:
show
| on GitHub
def to_yaml_type
"!tag:yaml.org,2002:omap"
end
Source:
show
| on GitHub
def values
@keys.collect { |key| self[key] }
end