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.

Public Class Methods

[](*args) click to toggle source
   # File lib/sinatra/indifferent_hash.rb
42 def self.[](*args)
43   new.merge!(Hash[*args])
44 end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
70 def [](key)
71   super(convert_key(key))
72 end
[]=(key, value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
74 def []=(key, value)
75   super(convert_key(key), convert_value(value))
76 end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
56 def assoc(key)
57   super(convert_key(key))
58 end
compact() click to toggle source
    # File lib/sinatra/indifferent_hash.rb
181 def compact
182   dup.tap(&:compact!)
183 end
default(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
46 def default(*args)
47   args.map!(&method(:convert_key))
48 
49   super(*args)
50 end
default=(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
52 def default=(value)
53   super(convert_value(value))
54 end
delete(key) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
 98 def delete(key)
 99   super(convert_key(key))
100 end
dig(key, *other_keys) click to toggle source

Added in Ruby 2.3

Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
103 def dig(key, *other_keys)
104   super(convert_key(key), *other_keys)
105 end
except(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
185 def except(*keys)
186   keys.map!(&method(:convert_key))
187 
188   self.class[super(*keys)]
189 end
fetch(key, *args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
64 def fetch(key, *args)
65   args.map!(&method(:convert_value))
66 
67   super(convert_key(key), *args)
68 end
fetch_values(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
107 def fetch_values(*keys)
108   keys.map!(&method(:convert_key))
109 
110   super(*keys)
111 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
80 def key(value)
81   super(convert_value(value))
82 end
key?(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
84 def key?(key)
85   super(convert_key(key))
86 end
Also aliased as: has_key?, include?, member?
member?(key)
Alias for: key?
merge(*other_hashes, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
143 def merge(*other_hashes, &block)
144   dup.merge!(*other_hashes, &block)
145 end
merge!(*other_hashes) { |key, self, value| ... } click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
125 def merge!(*other_hashes)
126   other_hashes.each do |other_hash|
127     if other_hash.is_a?(self.class)
128       super(other_hash)
129     else
130       other_hash.each_pair do |key, value|
131         key = convert_key(key)
132         value = yield(key, self[key], value) if block_given? && key?(key)
133         self[key] = convert_value(value)
134       end
135     end
136   end
137 
138   self
139 end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
60 def rassoc(value)
61   super(convert_value(value))
62 end
reject(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
175 def reject(*args, &block)
176   return to_enum(:reject) unless block_given?
177 
178   dup.tap { |hash| hash.reject!(*args, &block) }
179 end
replace(other_hash) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
147 def replace(other_hash)
148   super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
149 end
select(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
169 def select(*args, &block)
170   return to_enum(:select) unless block_given?
171 
172   dup.tap { |hash| hash.select!(*args, &block) }
173 end
slice(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
113 def slice(*keys)
114   keys.map!(&method(:convert_key))
115 
116   self.class[super(*keys)]
117 end
store(key, value)
Alias for: []=
transform_keys(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
160 def transform_keys(&block)
161   dup.transform_keys!(&block)
162 end
transform_keys!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
164 def transform_keys!
165   super
166   super(&method(:convert_key))
167 end
transform_values(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
151 def transform_values(&block)
152   dup.transform_values!(&block)
153 end
transform_values!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
155 def transform_values!
156   super
157   super(&method(:convert_value))
158 end
update(*other_hashes)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
92 def value?(value)
93   super(convert_value(value))
94 end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
119 def values_at(*keys)
120   keys.map!(&method(:convert_key))
121 
122   super(*keys)
123 end

Private Instance Methods

convert_key(key) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
193 def convert_key(key)
194   key.is_a?(Symbol) ? key.to_s : key
195 end
convert_value(value) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
197 def convert_value(value)
198   case value
199   when Hash
200     value.is_a?(self.class) ? value : self.class[value]
201   when Array
202     value.map(&method(:convert_value))
203   else
204     value
205   end
206 end