cicphash.rb

cicphash.rb
Last Update: 2021-12-10 10:40:35 -0800

Case Insensitive Case Preserving Hash

CICPHash has the same interface as Hash, but is case insensitive and case preserving. Any value can be used as a key. However, you cannot have two keys in the same CICPHash that would be the same if when converted to strings would be equal or differing only in case.

For example, all of the following keys would be considered equalivalent: 'ab', 'Ab', 'AB', 'aB', :ab, :Ab, :AB, :aB

CICPHash uses a last match wins policy. If an key-value pair is added to a CICPHash and a case insensitive variant of the key is already in the hash the instance of the key in the hash becomes the same the most recently added key (and the value is updated to reflect the new value).

You can change the rules determining which keys are equal by modifying the private convert_key method. By default, it is set to key.to_s.downcase. This method should produce the same output for any keys that you want to consider equal. For example, if you want :a and :A to be equal but :a to be different than “a”, maybe key.inspect.downcase would work for you.