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
40 def self.[](*args)
41   new.merge!(Hash[*args])
42 end
new(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
44 def initialize(*args)
45   args.map!(&method(:convert_value))
46 
47   super(*args)
48 end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
74 def [](key)
75   super(convert_key(key))
76 end
[]=(key, value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
78 def []=(key, value)
79   super(convert_key(key), convert_value(value))
80 end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
60 def assoc(key)
61   super(convert_key(key))
62 end
compact() click to toggle source
    # File lib/sinatra/indifferent_hash.rb
185 def compact
186   dup.tap(&:compact!)
187 end
default(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
50 def default(*args)
51   args.map!(&method(:convert_key))
52 
53   super(*args)
54 end
default=(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
56 def default=(value)
57   super(convert_value(value))
58 end
delete(key) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
102 def delete(key)
103   super(convert_key(key))
104 end
dig(key, *other_keys) click to toggle source

Added in Ruby 2.3

Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
107 def dig(key, *other_keys)
108   super(convert_key(key), *other_keys)
109 end
fetch(key, *args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
68 def fetch(key, *args)
69   args.map!(&method(:convert_value))
70 
71   super(convert_key(key), *args)
72 end
fetch_values(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
111 def fetch_values(*keys)
112   keys.map!(&method(:convert_key))
113 
114   super(*keys)
115 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
84 def key(value)
85   super(convert_value(value))
86 end
key?(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
88 def key?(key)
89   super(convert_key(key))
90 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
147 def merge(*other_hashes, &block)
148   dup.merge!(*other_hashes, &block)
149 end
merge!(*other_hashes) { |key, self, value| ... } click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
129 def merge!(*other_hashes)
130   other_hashes.each do |other_hash|
131     if other_hash.is_a?(self.class)
132       super(other_hash)
133     else
134       other_hash.each_pair do |key, value|
135         key = convert_key(key)
136         value = yield(key, self[key], value) if block_given? && key?(key)
137         self[key] = convert_value(value)
138       end
139     end
140   end
141 
142   self
143 end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
64 def rassoc(value)
65   super(convert_value(value))
66 end
reject(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
179 def reject(*args, &block)
180   return to_enum(:reject) unless block_given?
181 
182   dup.tap { |hash| hash.reject!(*args, &block) }
183 end
replace(other_hash) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
151 def replace(other_hash)
152   super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
153 end
select(*args, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
173 def select(*args, &block)
174   return to_enum(:select) unless block_given?
175 
176   dup.tap { |hash| hash.select!(*args, &block) }
177 end
slice(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
117 def slice(*keys)
118   keys.map!(&method(:convert_key))
119 
120   self.class[super(*keys)]
121 end
store(key, value)
Alias for: []=
transform_keys(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
164 def transform_keys(&block)
165   dup.transform_keys!(&block)
166 end
transform_keys!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
168 def transform_keys!
169   super
170   super(&method(:convert_key))
171 end
transform_values(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
155 def transform_values(&block)
156   dup.transform_values!(&block)
157 end
transform_values!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
159 def transform_values!
160   super
161   super(&method(:convert_value))
162 end
update(*other_hashes)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
96 def value?(value)
97   super(convert_value(value))
98 end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
123 def values_at(*keys)
124   keys.map!(&method(:convert_key))
125 
126   super(*keys)
127 end

Private Instance Methods

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