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
47 def self.[](*args)
48   new.merge!(Hash[*args])
49 end
new(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
51 def initialize(*args)
52   args.map!(&method(:convert_value))
53 
54   super(*args)
55 end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
81 def [](key)
82   super(convert_key(key))
83 end
[]=(key, value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
85 def []=(key, value)
86   super(convert_key(key), convert_value(value))
87 end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
67 def assoc(key)
68   super(convert_key(key))
69 end
default(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
57 def default(*args)
58   args.map!(&method(:convert_key))
59 
60   super(*args)
61 end
default=(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
63 def default=(value)
64   super(convert_value(value))
65 end
delete(key) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
109 def delete(key)
110   super(convert_key(key))
111 end
dig(key, *other_keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
113 def dig(key, *other_keys)
114   super(convert_key(key), *other_keys)
115 end
fetch(key, *args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
75 def fetch(key, *args)
76   args.map!(&method(:convert_value))
77 
78   super(convert_key(key), *args)
79 end
fetch_values(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
117 def fetch_values(*keys)
118   keys.map!(&method(:convert_key))
119 
120   super(*keys)
121 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
91 def key(value)
92   super(convert_value(value))
93 end
key?(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
95 def key?(key)
96   super(convert_key(key))
97 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
149 def merge(other_hash, &block)
150   dup.merge!(other_hash, &block)
151 end
merge!(other_hash) { |key, self, value| ... } click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
135 def merge!(other_hash)
136   return super if other_hash.is_a?(self.class)
137 
138   other_hash.each_pair do |key, value|
139     key = convert_key(key)
140     value = yield(key, self[key], value) if block_given? && key?(key)
141     self[key] = convert_value(value)
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
71 def rassoc(value)
72   super(convert_value(value))
73 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
slice(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
123 def slice(*keys)
124   keys.map!(&method(:convert_key))
125 
126   self.class[super(*keys)]
127 end
store(key, value)
Alias for: []=
transform_keys(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
169 def transform_keys(&block)
170   dup.transform_keys!(&block)
171 end
transform_keys!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
173 def transform_keys!
174   super
175   super(&method(:convert_key))
176 end
transform_values(&block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
158 def transform_values(&block)
159   dup.transform_values!(&block)
160 end
transform_values!() click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
162 def transform_values!
163   super
164   super(&method(:convert_value))
165 end
update(other_hash)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
103 def value?(value)
104   super(convert_value(value))
105 end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
129 def values_at(*keys)
130   keys.map!(&method(:convert_key))
131 
132   super(*keys)
133 end

Private Instance Methods

convert_key(key) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
181 def convert_key(key)
182   key.is_a?(Symbol) ? key.to_s : key
183 end
convert_value(value) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
185 def convert_value(value)
186   case value
187   when Hash
188     value.is_a?(self.class) ? value : self.class[value]
189   when Array
190     value.map(&method(:convert_value))
191   else
192     value
193   end
194 end