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 39 def self.[](*args) new.merge!(Hash[*args]) end
new(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 43 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 67 def [](key) super(convert_key(key)) end
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 71 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 55 def assoc(key) super(convert_key(key)) end
default(*args)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 47 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 51 def default=(value) super(convert_value(value)) end
delete(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 95 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 99 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 63 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 103 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 77 def key(value) super(convert_value(value)) end
key?(key)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 81 def key?(key) super(convert_key(key)) end
merge(other_hash, &block)
click to toggle source
# File lib/sinatra/indifferent_hash.rb, line 125 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 111 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 59 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 129 def replace(other_hash) super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) end
value?(value)
click to toggle source
Calls superclass method
# File lib/sinatra/indifferent_hash.rb, line 89 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 107 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 135 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 139 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