Class: Sinatra::IndifferentHash
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Hash
|
|
Instance Chain:
self,
Hash
|
|
Inherits: |
Hash
|
Defined in: | lib/sinatra/indifferent_hash.rb |
Overview
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 hash[: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 hash[: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
.
Class Method Summary
Instance Method Summary
- #[](key)
- #[]=(key, value) (also: #store)
- #assoc(key)
- #compact
- #default(*args)
- #default=(value)
- #delete(key)
-
#dig(key, *other_keys)
Added in Ruby 2.3.
- #except(*keys)
- #fetch(key, *args)
- #fetch_values(*keys)
-
#has_key?(key)
Alias for #key?.
-
#has_value?(value)
Alias for #value?.
-
#include?(key)
Alias for #key?.
- #key(value)
- #key?(key) ⇒ Boolean (also: #has_key?, #include?, #member?)
-
#member?(key)
Alias for #key?.
- #merge(*other_hashes, &block)
- #merge!(*other_hashes) (also: #update)
- #rassoc(value)
- #reject(*args, &block)
- #replace(other_hash)
- #select(*args, &block)
- #slice(*keys)
-
#store(key, value)
Alias for #[]=.
- #transform_keys(&block)
- #transform_keys!
- #transform_values(&block)
- #transform_values!
-
#update(*other_hashes)
Alias for #merge!.
- #value?(value) ⇒ Boolean (also: #has_value?)
- #values_at(*keys)
- #convert_key(key) private
- #convert_value(value) private
Class Method Details
.[](*args)
[ GitHub ]Instance Method Details
#[](key)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 70
def [](key) super(convert_key(key)) end
#[]=(key, value) Also known as: #store
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 74
def []=(key, value) super(convert_key(key), convert_value(value)) end
#assoc(key)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 56
def assoc(key) super(convert_key(key)) end
#compact
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 181
def compact dup.tap(&:compact!) end
#convert_key(key) (private)
[ GitHub ]#convert_value(value) (private)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 197
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
#default(*args)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 46
def default(*args) args.map!(&method(:convert_key)) super(*args) end
#default=(value)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 52
def default=(value) super(convert_value(value)) end
#delete(key)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 98
def delete(key) super(convert_key(key)) end
#dig(key, *other_keys)
Added in Ruby 2.3
# File 'lib/sinatra/indifferent_hash.rb', line 103
def dig(key, *other_keys) super(convert_key(key), *other_keys) end
#except(*keys)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 185
def except(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end
#fetch(key, *args)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 64
def fetch(key, *args) args.map!(&method(:convert_value)) super(convert_key(key), *args) end
#fetch_values(*keys)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 107
def fetch_values(*keys) keys.map!(&method(:convert_key)) super(*keys) end
#has_key?(key)
Alias for #key?.
# File 'lib/sinatra/indifferent_hash.rb', line 88
alias has_key? key?
#has_value?(value)
Alias for #value?.
# File 'lib/sinatra/indifferent_hash.rb', line 96
alias has_value? value?
#include?(key)
Alias for #key?.
# File 'lib/sinatra/indifferent_hash.rb', line 89
alias include? key?
#key(value)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 80
def key(value) super(convert_value(value)) end
#key?(key) ⇒ Boolean
Also known as: #has_key?, #include?, #member?
# File 'lib/sinatra/indifferent_hash.rb', line 84
def key?(key) super(convert_key(key)) end
#member?(key)
Alias for #key?.
# File 'lib/sinatra/indifferent_hash.rb', line 90
alias member? key?
#merge(*other_hashes, &block)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 143
def merge(*other_hashes, &block) dup.merge!(*other_hashes, &block) end
#merge!(*other_hashes) Also known as: #update
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 125
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
#rassoc(value)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 60
def rassoc(value) super(convert_value(value)) end
#reject(*args, &block)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 175
def reject(*args, &block) return to_enum(:reject) unless block_given? dup.tap { |hash| hash.reject!(*args, &block) } end
#replace(other_hash)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 147
def replace(other_hash) super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash]) end
#select(*args, &block)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 169
def select(*args, &block) return to_enum(:select) unless block_given? dup.tap { |hash| hash.select!(*args, &block) } end
#slice(*keys)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 113
def slice(*keys) keys.map!(&method(:convert_key)) self.class[super(*keys)] end
#store(key, value)
Alias for #[]=.
# File 'lib/sinatra/indifferent_hash.rb', line 78
alias store []=
#transform_keys(&block)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 160
def transform_keys(&block) dup.transform_keys!(&block) end
#transform_keys!
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 164
def transform_keys! super super(&method(:convert_key)) end
#transform_values(&block)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 151
def transform_values(&block) dup.transform_values!(&block) end
#transform_values!
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 155
def transform_values! super super(&method(:convert_value)) end
#update(*other_hashes)
Alias for #merge!.
# File 'lib/sinatra/indifferent_hash.rb', line 141
alias update merge!
#value?(value) ⇒ Boolean
Also known as: #has_value?
# File 'lib/sinatra/indifferent_hash.rb', line 92
def value?(value) super(convert_value(value)) end
#values_at(*keys)
[ GitHub ]# File 'lib/sinatra/indifferent_hash.rb', line 119
def values_at(*keys) keys.map!(&method(:convert_key)) super(*keys) end