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
compact()
click to toggle source
# File lib/sinatra/indifferent_hash.rb 193 def compact 194 dup.tap(&:compact!) 195 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
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
merge(*other_hashes, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb 153 def merge(*other_hashes, &block) 154 dup.merge!(*other_hashes, &block) 155 end
merge!(*other_hashes) { |key, self, value| ... }
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb 135 def merge!(*other_hashes) 136 other_hashes.each do |other_hash| 137 if other_hash.is_a?(self.class) 138 super(other_hash) 139 else 140 other_hash.each_pair do |key, value| 141 key = convert_key(key) 142 value = yield(key, self[key], value) if block_given? && key?(key) 143 self[key] = convert_value(value) 144 end 145 end 146 end 147 148 self 149 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
reject(*args, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb 188 def reject(*args, &block) 189 return to_enum(:reject) unless block_given? 190 dup.tap { |hash| hash.reject!(*args, &block) } 191 end
replace(other_hash)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb 157 def replace(other_hash) 158 super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) 159 end
select(*args, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb 183 def select(*args, &block) 184 return to_enum(:select) unless block_given? 185 dup.tap { |hash| hash.select!(*args, &block) } 186 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
transform_keys(&block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb 173 def transform_keys(&block) 174 dup.transform_keys!(&block) 175 end
transform_keys!()
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb 177 def transform_keys! 178 super 179 super(&method(:convert_key)) 180 end
transform_values(&block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb 162 def transform_values(&block) 163 dup.transform_values!(&block) 164 end
transform_values!()
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb 166 def transform_values! 167 super 168 super(&method(:convert_value)) 169 end
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 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