Class Hashery::Dictionary
In: lib/hashery/dictionary.rb
Parent: Object

The Dictionary class is a Hash that preserves order. So it has some array-like extensions also. By defualt a Dictionary object preserves insertion order, but any order can be specified including alphabetical key order.

Using a Dictionary is almost the same as using a Hash.

  # You can do simply
  hsh = Dictionary.new
  hsh['z'] = 1
  hsh['a'] = 2
  hsh['c'] = 3
  p hsh.keys     #=> ['z','a','c']

  # or using Dictionary[] method
  hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
  p hsh.keys     #=> ['z','a','c']

  # but this don't preserve order
  hsh = Dictionary['z'=>1, 'a'=>2, 'c'=>3]
  p hsh.keys     #=> ['a','c','z']

  # Dictionary 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

Notes

  • You can use order_by to set internal sort order.
  • #<< takes a two element [k,v] array and inserts.
  • Use ::auto which creates Dictionay sub-entries as needed.
  • And ::alpha which creates a new Dictionary sorted by key.

Acknowledgments

Dictionary is a port of OrderHash 2.0 Copyright (c) 2005 Jan Molic.

People who have contributed to this class since then include:

OrderedHash is public domain.

Methods

<<   ==   []   []   []=   alphabetic   auto   clear   delete   delete_if   dup   each   each_key   each_pair   each_value   empty?   fetch   first   has_key?   hash_table   insert   inspect   invert   key?   keys   last   length   merge   merge!   new   new_by   order   order_by   order_by_key   order_by_value   pop   push   reject   reject!   reorder   replace   reverse   reverse!   select   shift   size   store   to_a   to_h   to_hash   to_s   unshift   update   values  

Included Modules

Enumerable

External Aliases

alphabetic -> alpha
  DEPRECATED: Use alphabetic instead.

Public Class methods

Create a new Dictionary storing argument pairs as an initial mapping.

TODO: Is this needed? Doesn‘t the super class do this?

Returns Dictionary instance.

Alternate to new which creates a dictionary sorted by the key as a string.

  d = Dictionary.alphabetic
  d["z"] = 1
  d["y"] = 2
  d["x"] = 3
  d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

  Dictionary.new.order_by { |key,value| key.to_s }

Alternate to new which auto-creates sub-dictionaries as needed.

Examples

  d = Dictionary.auto
  d["a"]["b"]["c"] = "abc"  #=> { "a"=>{"b"=>{"c"=>"abc"}}}

New Dictiionary.

Like new but the block sets the order instead of the default.

  Dictionary.new_by{ |k,v| k }

Public Instance methods

Same as push.

Is the dictionary instance equivalent to another?

Lookup entry with key.

Store operator.

  h[key] = value

Or with additional index.

 h[key,index] = value

Clear dictionary of all entries.

Delete the entry with given key.

Delete entry if it fits conditional block.

Duplicate dictionary.

Returns [Dictionary].

Iterate over each key-value pair.

Iterate over each key.

each_pair()

Alias for each

Is the dictionary empty?

Returns `true` or `false`.

Featch entry given key.

Get/set initial entry value.

Does the dictionary have a given key.

Returns `true` or `false`.

Insert entry into dictionary at specific index position.

index - [Integer] Position of order placement. key - [Object] Key to associate with value. value - [Object] Value to associate with key.

Returns `value` stored.

Inspection string for Dictionary.

Returns [String].

Invert the dictionary.

Returns [Dictionary] New dictionary that is inverse of the original.

Does the dictionary have a given key.

Returns `true` or `false`.

List of all dictionary keys.

Returns [Array].

Get/set last entry value.

Number of items in the dictionary.

Merge other hash creating new dictionary.

Returns [Dictionary].

merge!( hsh2 )

Alias for update

Order of keys.

Returns [Array].

Keep dictionary sorted by a specific sort order.

block - Ordering procedure.

Returns self.

Keep dictionary sorted by key.

  d = Dictionary.new.order_by_key
  d["z"] = 1
  d["y"] = 2
  d["x"] = 3
  d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

  Dictionary.new.order_by { |key,value| key }

The initializer Dictionary#alpha also provides this.

Returns self.

Keep dictionary sorted by value.

  d = Dictionary.new.order_by_value
  d["z"] = 1
  d["y"] = 2
  d["x"] = 3
  d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

  Dictionary.new.order_by { |key,value| value }

Pop entry off the bottom of dictionary.

Push entry on to bottom of the dictionary.

Reject entries based on give condition block and return new dictionary.

Returns [Dictionary].

Reject entries based on give condition block.

Returns [Hash] of rejected entries.

FIXME: This looks like it is implemented wrong!!!

Re-apply the sorting procedure.

Replace dictionary entries with new table.

Reverse the order of duplicte dictionary.

Returns [Dictionary].

Reverse the order of the dictionary.

Returns self.

Select items from dictiornary.

Returns [Array] of two-element arrays.

Remove entry from the to top of dictionary.

size()

Alias for length

Add entry into dictionary.

Returns `value`.

Convert to array.

Returns [Array] of two-element arrays.

Get a duplicate of the underlying hash table.

Returns [Hash].

Get a duplicate of the underlying hash table.

Returns [Hash].

Convert to array then to string.

Returns [String].

Push entry on to the top of dictionary.

Update dictionary with other hash.

Returns self.

List of all dictionary values.

Returns [Array].

Protected Instance methods

Underlying hash table.

[Validate]