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 38 def self.[](*args) new.merge!(Hash[*args]) end
new(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 42 def initialize(*args) super(*args.map(&method(:convert_value))) end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 66 def [](key) super(convert_key(key)) end
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 70 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 54 def assoc(key) super(convert_key(key)) end
default(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 46 def default(*args) super(*args.map(&method(:convert_key))) end
default=(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 50 def default=(value) super(convert_value(value)) end
delete(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 94 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 98 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 62 def fetch(key, *args) super(convert_key(key), *args.map(&method(:convert_value))) end
fetch_values(*keys)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 102 def fetch_values(*keys) super(*keys.map(&method(:convert_key))) end
key(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 76 def key(value) super(convert_value(value)) end
key?(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 80 def key?(key) super(convert_key(key)) end
merge(other_hash, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 129 def merge(other_hash, &block) dup.merge!(other_hash, &block) end
merge!(other_hash) { |key, self, value| ... }
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 115 def merge!(other_hash) return super if other_hash.is_a?(self.class) 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 self end
Also aliased as: update
rassoc(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 58 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 133 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 106 def slice(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end
value?(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 88 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 111 def values_at(*keys) super(*keys.map(&method(:convert_key))) end
Private Instance Methods
convert_key(key)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 139 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 143 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