Class: Jinx::CaseInsensitiveHash

Inherits:
Hash show all
Defined in:
lib/jinx/helpers/case_insensitive_hash.rb

Overview

CaseInsensitiveHash accesses entries in a case-insensitive String comparison. The accessor method key argument is converted to a String before look-up.

Examples:

hash = CaseInsensitiveHash.new
hash[:UP] = "down"
hash['up'] #=> "down"

Instance Method Summary collapse

Methods inherited from Hash

#base__merge!, #merge, #merge!

Methods included from Hasher

#==, #assoc_values, #compact, #compose, #copy_recursive, #detect_hash_value, #detect_key, #detect_key_with_value, #detect_value, #difference, #each_key, #each_pair, #each_value, #enum_keys, #enum_keys_with_value, #enum_values, #filter, #filter_on_key, #filter_on_value, #flatten, #has_value?, #inspect, #join, #keys, #pretty_print, #pretty_print_cycle, #qp, #reject_keys, #reject_values, #select_keys, #select_values, #sort, #split, #to_hash, #to_s, #to_set, #transform_key, #transform_value, #union, #values

Methods included from Enumerable

#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure

Methods included from Collection

#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union

Constructor Details

#initializeCaseInsensitiveHash

Returns a new instance of CaseInsensitiveHash.



10
11
12
# File 'lib/jinx/helpers/case_insensitive_hash.rb', line 10

def initialize
  super
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/jinx/helpers/case_insensitive_hash.rb', line 14

def [](key)
  # if there is lower-case key association, then convert to lower-case and return.
  # otherwise, delegate to super with the call argument unchanged. this ensures
  # that a default block passed to the constructor will be called with the correct
  # key argument.
  has_key?(key) ? super(key.to_s.downcase) : super(key)
end

#[]=(key, value) ⇒ Object Also known as: store



22
23
24
# File 'lib/jinx/helpers/case_insensitive_hash.rb', line 22

def []=(key, value)
  super(key.to_s.downcase, value)
end

#delete(key) ⇒ Object



30
31
32
# File 'lib/jinx/helpers/case_insensitive_hash.rb', line 30

def delete(key)
  super(key.to_s.downcase)
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Returns:



26
27
28
# File 'lib/jinx/helpers/case_insensitive_hash.rb', line 26

def has_key?(key)
  super(key.to_s.downcase)
end