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
new(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
46 def initialize(*args)
47   args.map!(&method(:convert_value))
48 
49   super(*args)
50 end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
76 def [](key)
77   super(convert_key(key))
78 end
[]=(key, value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
80 def []=(key, value)
81   super(convert_key(key), convert_value(value))
82 end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
62 def assoc(key)
63   super(convert_key(key))
64 end
compact() click to toggle source
    # File lib/sinatra/indifferent_hash.rb
187 def compact
188   dup.tap(&:compact!)
189 end
default(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
52 def default(*args)
53   args.map!(&method(:convert_key))
54 
55   super(*args)
56 end
default=(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
58 def default=(value)
59   super(convert_value(value))
60 end
delete(key) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
104 def delete(key)
105   super(convert_key(key))
106 end
dig(key, *other_keys) click to toggle source

Added in Ruby 2.3

Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
109 def dig(key, *other_keys)
110   super(convert_key(key), *other_keys)
111 end
except(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
191 def except(*keys)
192   keys.map!(&method(:convert_key))
193 
194   super(*keys)
195 end
fetch(key, *args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
70 def fetch(key, *args)
71   args.map!(&method(:convert_value))
72 
73   super(convert_key(key), *args)
74 end
fetch_values(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
113 def fetch_values(*keys)
114   keys.map!(&method(:convert_key))
115 
116   super(*keys)
117 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
86 def key(value)
87   super(convert_value(value))
88 end
key?(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
90 def key?(key)
91   super(convert_key(key))
92 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
149 def merge(*other_hashes, &block)
150   dup.merge!(*other_hashes, &block)
151 end
merge!(*other_hashes) { |key, self, value| ... } click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
131 def merge!(*other_hashes)
132   other_hashes.each do |other_hash|
133     if other_hash.is_a?(self.class)
134       super(other_hash)
135     else
136       other_hash.each_pair do |key, value|
137         key = convert_key(key)
138         value = yield(key, self[key], value) if block_given? && key?(key)
139         self[key] = convert_value(value)
140       end
141     end
142   end
143 
144   self
145 end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
66 def rassoc(value)
67   super(convert_value(value))
68 end
reject(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
181 def reject(*args, &block)
182   return to_enum(:reject) unless block_given?
183 
184   dup.tap { |hash| hash.reject!(*args, &block) }
185 end
replace(other_hash) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
153 def replace(other_hash)
154   super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
155 end
select(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
175 def select(*args, &block)
176   return to_enum(:select) unless block_given?
177 
178   dup.tap { |hash| hash.select!(*args, &block) }
179 end
slice(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
119 def slice(*keys)
120   keys.map!(&method(:convert_key))
121 
122   self.class[super(*keys)]
123 end
store(key, value)
Alias for: []=
transform_keys(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
166 def transform_keys(&block)
167   dup.transform_keys!(&block)
168 end
transform_keys!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
170 def transform_keys!
171   super
172   super(&method(:convert_key))
173 end
transform_values(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
157 def transform_values(&block)
158   dup.transform_values!(&block)
159 end
transform_values!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
161 def transform_values!
162   super
163   super(&method(:convert_value))
164 end
update(*other_hashes)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
 98 def value?(value)
 99   super(convert_value(value))
100 end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
125 def values_at(*keys)
126   keys.map!(&method(:convert_key))
127 
128   super(*keys)
129 end

Private Instance Methods

convert_key(key) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
199 def convert_key(key)
200   key.is_a?(Symbol) ? key.to_s : key
201 end
convert_value(value) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
203 def convert_value(value)
204   case value
205   when Hash
206     value.is_a?(self.class) ? value : self.class[value]
207   when Array
208     value.map(&method(:convert_value))
209   else
210     value
211   end
212 end