| Description: |
# You can do simply
hsh = OrderedHash.new
hsh['z'] = 1
hsh['a'] = 2
hsh['c'] = 3
p hsh.keys # ['z','a','c']
# or using OrderedHash[] method
hsh = OrderedHash['z', 1, 'a', 2, 'c', 3]
p hsh.keys # ['z','a','c']
# but this don't preserve order
hsh = OrderedHash['z'=>1, 'a'=>2, 'c'=>3]
p hsh.keys # ['a','c','z']
# OrderedHash has useful extensions: push, pop and unshift
p hsh.push('to_end', 15) # true, key added
p hsh.push('to_end', 30) # false, already - nothing happen
p hsh.unshift('to_begin', 50) # true, key added
p hsh.unshift('to_begin', 60) # false, already - nothing happen
p hsh.keys # ["to_begin", "a", "c", "z", "to_end"]
p hsh.pop # ["to_end", 15], if nothing remains, return nil
p hsh.keys # ["to_begin", "a", "c", "z"]
p hsh.shift # ["to_begin", 30], if nothing remains, return nil
|