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, line 47 def self.[](*args) new.merge!(Hash[*args]) end
new(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 51 def initialize(*args) args.map!(&method(:convert_value)) super(*args) end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 81 def [](key) super(convert_key(key)) end
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 85 def []=(key, value) super(convert_key(key), convert_value(value)) end
Also aliased as: store
assoc(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 67 def assoc(key) super(convert_key(key)) end
default(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 57 def default(*args) args.map!(&method(:convert_key)) super(*args) end
default=(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 63 def default=(value) super(convert_value(value)) end
delete(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 109 def delete(key) super(convert_key(key)) end
dig(key, *other_keys)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 113 def dig(key, *other_keys) super(convert_key(key), *other_keys) end
fetch(key, *args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 75 def fetch(key, *args) args.map!(&method(:convert_value)) super(convert_key(key), *args) end
fetch_values(*keys)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 117 def fetch_values(*keys) keys.map!(&method(:convert_key)) super(*keys) end
key(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 91 def key(value) super(convert_value(value)) end
key?(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 95 def key?(key) super(convert_key(key)) end
merge(*other_hashes, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 153 def merge(*other_hashes, &block) dup.merge!(*other_hashes, &block) end
merge!(*other_hashes) { |key, self, value| ... }
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 135 def merge!(*other_hashes) other_hashes.each do |other_hash| if other_hash.is_a?(self.class) super(other_hash) else other_hash.each_pair do |key, value| key = convert_key(key) value = yield(key, self[key], value) if block_given? && key?(key) self[key] = convert_value(value) end end end self end
Also aliased as: update
rassoc(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 71 def rassoc(value) super(convert_value(value)) end
replace(other_hash)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 157 def replace(other_hash) super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) end
slice(*keys)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 123 def slice(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end
transform_keys(&block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 173 def transform_keys(&block) dup.transform_keys!(&block) end
transform_keys!()
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 177 def transform_keys! super super(&method(:convert_key)) end
transform_values(&block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 162 def transform_values(&block) dup.transform_values!(&block) end
transform_values!()
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 166 def transform_values! super super(&method(:convert_value)) end
value?(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 103 def value?(value) super(convert_value(value)) end
Also aliased as: has_value?
values_at(*keys)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 129 def values_at(*keys) keys.map!(&method(:convert_key)) super(*keys) end
Private Instance Methods
convert_key(key)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 185 def convert_key(key) key.is_a?(Symbol) ? key.to_s : key end
convert_value(value)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 189 def convert_value(value) case value when Hash value.is_a?(self.class) ? value : self.class[value] when Array value.map(&method(:convert_value)) else value end end