class Sinatra::IndifferentHash
example the params
hash in Sinatra.
expected keys and it is convenient to understand both as the same. For
But this class is intended for use cases where strings or symbols are the
hash # => { “a”=>1, 0=>0 }
= 0<br>hash = 1
hash = Sinatra::IndifferentHash
Technically other types of keys are accepted:
hash.keys # => [“a”]
You are guaranteed that the key is returned as a string:<br><br>hash = 1
hash = Sinatra::IndifferentHash.new
belongs to the public interface. For example, given:
writing interface (calling e.g. []=
, merge
). This mapping
Internally, symbols are mapped to strings when used as keys in the entire<br><br>rgb # => ‘#FFFFFF’ # string retrieval<br>rgb # => ‘#FFFFFF’ # symbol retrieval<br>rgb = ‘#FFFFFF’ # string assignment<br><br>rgb # => ‘#000000’ # string retrieval<br>rgb # => ‘#000000’ # symbol retrieval<br>rgb = ‘#000000’ # symbol assignment
rgb = Sinatra::IndifferentHash.new
considered to be the same.
Implements a hash where keys :foo
and "foo"
are
stuff removed.
A poor man’s ActiveSupport::HashWithIndifferentAccess, with all the Rails-y
def self.[](*args)
def self.[](*args) new.merge!(Hash[*args]) end
def [](key)
def [](key) super(convert_key(key)) end
def []=(key, value)
def []=(key, value) super(convert_key(key), convert_value(value)) end
def assoc(key)
def assoc(key) super(convert_key(key)) end
def compact
def compact dup.tap(&:compact!) end
def convert_key(key)
def convert_key(key) key.is_a?(Symbol) ? key.to_s : key end
def convert_value(value)
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
def default(*args)
def default(*args) args.map!(&method(:convert_key)) super(*args) end
def default=(value)
def default=(value) super(convert_value(value)) end
def delete(key)
def delete(key) super(convert_key(key)) end
def dig(key, *other_keys)
def dig(key, *other_keys) super(convert_key(key), *other_keys) end
def except(*keys)
def except(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0")
def fetch(key, *args)
def fetch(key, *args) args.map!(&method(:convert_value)) super(convert_key(key), *args) end
def fetch_values(*keys)
def fetch_values(*keys) keys.map!(&method(:convert_key)) super(*keys) end
def key(value)
def key(value) super(convert_value(value)) end
def key?(key)
def key?(key) super(convert_key(key)) end
def merge(*other_hashes, &block)
def merge(*other_hashes, &block) dup.merge!(*other_hashes, &block) end
def merge!(*other_hashes)
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
def rassoc(value)
def rassoc(value) super(convert_value(value)) end
def reject(*args, &block)
def reject(*args, &block) return to_enum(:reject) unless block_given? dup.tap { |hash| hash.reject!(*args, &block) } end
def replace(other_hash)
def replace(other_hash) super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) end
def select(*args, &block)
def select(*args, &block) return to_enum(:select) unless block_given? dup.tap { |hash| hash.select!(*args, &block) } end
def slice(*keys)
def slice(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end
def transform_keys(&block)
def transform_keys(&block) dup.transform_keys!(&block) end
def transform_keys!
def transform_keys! super super(&method(:convert_key)) end
def transform_values(&block)
def transform_values(&block) dup.transform_values!(&block) end
def transform_values!
def transform_values! super super(&method(:convert_value)) end
def value?(value)
def value?(value) super(convert_value(value)) end
def values_at(*keys)
def values_at(*keys) keys.map!(&method(:convert_key)) super(*keys) end