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
hash = Sinatra::IndifferentHash.new(:a=>1)
Technically other types of keys are accepted:
hash.keys # => [“a”]
You are guaranteed that the key is returned as a string:
hash = Sinatra::IndifferentHash.new(:a=>1)
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 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) super(*args.map(&method(:convert_key))) 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 if method_defined?(:dig) # Added in Ruby 2.3
def fetch(key, *args)
def fetch(key, *args) super(convert_key(key), *args.map(&method(:convert_value))) end
def fetch_values(*keys)
def fetch_values(*keys) super(*keys.map(&method(:convert_key))) end if method_defined?(:fetch_values) # Added in Ruby 2.3
def initialize(*args)
def initialize(*args) super(*args.map(&method(:convert_value))) 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_hash, &block)
def merge(other_hash, &block) dup.merge!(other_hash, &block) end
def merge!(other_hash)
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
def rassoc(value)
def rassoc(value) super(convert_value(value)) end
def replace(other_hash)
def replace(other_hash) super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) end
def value?(value)
def value?(value) super(convert_value(value)) end
def values_at(*keys)
def values_at(*keys) super(*keys.map(&method(:convert_key))) end