Module: Collapsium::IndifferentAccess

Defined in:
lib/collapsium/indifferent_access.rb

Overview

Provides indifferent access to string/symbol keys in a Hash. That is, if your hash contains a string key, you can also access it via a symbol and vice versa.

Constant Summary collapse

DEFAULT_PROC =

Set your Hash’s #default_proc to DEFAULT_PROC, and you’ve got indifferent access.

proc do |hash, key|
  case key
  when String
    sym = key.to_sym
    hash[sym] if hash.key?(sym)
  when Symbol
    str = key.to_s
    hash[str] if hash.key?(str)
  end
end.freeze