Make OrderedHash#to_a preserve order
This commit is contained in:
parent
16ea148ab1
commit
83f2bdf472
|
@ -71,6 +71,11 @@ class OrderedHash < Hash
|
|||
end
|
||||
alias :each_pair :each
|
||||
|
||||
def to_a
|
||||
@ordered_keys ||= []
|
||||
@ordered_keys.map { |k| [k, self[k]] }
|
||||
end
|
||||
|
||||
def values
|
||||
collect { |k, v| v }
|
||||
end
|
||||
|
|
|
@ -76,6 +76,10 @@ class OrderedHashTest < Test::Unit::TestCase
|
|||
assert_equal @ordered_keys, @oh.keys
|
||||
end
|
||||
|
||||
def test_to_a_order_preserved
|
||||
assert_equal @ordered_keys, @oh.to_a.map(&:first)
|
||||
end
|
||||
|
||||
def test_order_preserved_after_replace
|
||||
@oh['a'] = 42
|
||||
assert_equal @ordered_keys, @oh.keys
|
||||
|
|
Loading…
Reference in New Issue