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(: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.new(: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.

Public Class Methods

[](*args) click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 38
def self.[](*args)
  new.merge!(Hash[*args])
end
new(*args) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 42
def initialize(*args)
  super(*args.map(&method(:convert_value)))
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 66
def [](key)
  super(convert_key(key))
end
[]=(key, value) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 70
def []=(key, value)
  super(convert_key(key), convert_value(value))
end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 54
def assoc(key)
  super(convert_key(key))
end
default(*args) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 46
def default(*args)
  super(*args.map(&method(:convert_key)))
end
default=(value) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 50
def default=(value)
  super(convert_value(value))
end
delete(key) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 94
def delete(key)
  super(convert_key(key))
end
dig(key, *other_keys) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 98
def dig(key, *other_keys)
  super(convert_key(key), *other_keys)
end
fetch(key, *args) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 62
def fetch(key, *args)
  super(convert_key(key), *args.map(&method(:convert_value)))
end
fetch_values(*keys) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 102
def fetch_values(*keys)
  super(*keys.map(&method(:convert_key)))
end
has_key?(key)
Alias for: key?
has_value?(value)
Alias for: value?
include?(key)
Alias for: key?
key(value) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 76
def key(value)
  super(convert_value(value))
end
key?(key) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 80
def key?(key)
  super(convert_key(key))
end
Also aliased as: has_key?, include?, member?
member?(key)
Alias for: key?
merge(other_hash, &block) click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 124
def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end
merge!(other_hash) { |key, self, value| ... } click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 110
def merge!(other_hash)
  return super if other_hash.is_a?(self.class)

  other_hash.each_pair do |key, value|
    key = convert_key(key)
    value = yield(key, self[key], value) if block_given? && key?(key)
    self[key] = convert_value(value)
  end

  self
end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 58
def rassoc(value)
  super(convert_value(value))
end
replace(other_hash) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 128
def replace(other_hash)
  super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end
store(key, value)
Alias for: []=
update(other_hash)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 88
def value?(value)
  super(convert_value(value))
end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 106
def values_at(*keys)
  super(*keys.map(&method(:convert_key)))
end

Private Instance Methods

convert_key(key) click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 134
def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end
convert_value(value) click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 138
def convert_value(value)
  case value
  when Hash
    value.is_a?(self.class) ? value : self.class[value]
  when Array
    value.map(&method(:convert_value))
  else
    value
  end
end