class Sinatra::IndifferentHash

A poor man’s ActiveSupport::HashWithIndifferentAccess, with all the Rails-y stuff removed.

Implements a hash where keys :foo and "foo" are considered to be the same.

rgb = Sinatra::IndifferentHash.new

rgb[:black]    =  '#000000' # symbol assignment
rgb[:black]  # => '#000000' # symbol retrieval
rgb['black'] # => '#000000' # string retrieval

rgb['white']   =  '#FFFFFF' # string assignment
rgb[:white]  # => '#FFFFFF' # symbol retrieval
rgb['white'] # => '#FFFFFF' # string retrieval

Internally, symbols are mapped to strings when used as keys in the entire writing interface (calling e.g. []=, merge). This mapping belongs to the public interface. For example, given:

hash = Sinatra::IndifferentHash.new
hash[:a] = 1

You are guaranteed that the key is returned as a string:

hash.keys # => ["a"]

Technically other types of keys are accepted:

hash = Sinatra::IndifferentHash
hash[:a] = 1
hash[0] = 0
hash # => { "a"=>1, 0=>0 }

But this class is intended for use cases where strings or symbols are the expected keys and it is convenient to understand both as the same. For example the params hash in Sinatra.