Class: Sinatra::IndifferentHash
- Inherits:
-
Hash
show all
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/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' rgb[:black] rgb['black']
rgb['white'] = '#FFFFFF' rgb[:white] rgb['white']
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
Technically other types of keys are accepted:
hash = Sinatra::IndifferentHash.new(:a=>1)
hash[0] = 0
hash
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
collapse
Instance Method Summary
collapse
Methods inherited from Hash
#as_json, #assert_valid_keys, #compact_blank, #compact_blank!, #deep_dup, #deep_merge, #deep_merge!, #deep_stringify_keys, #deep_stringify_keys!, #deep_symbolize_keys, #deep_symbolize_keys!, #deep_transform_keys, #deep_transform_keys!, #deep_transform_values, #deep_transform_values!, #except, #except!, #extract!, #extractable_options?, from_trusted_xml, from_xml, #reverse_merge, #reverse_merge!, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_query, #to_xml, #with_indifferent_access
Constructor Details
Returns a new instance of IndifferentHash.
44
45
46
47
48
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 44
def initialize(*args)
args.map!(&method(:convert_value))
super(*args)
end
|
Class Method Details
.[](*args) ⇒ Object
40
41
42
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 40
def self.[](*args)
new.merge!(Hash[*args])
end
|
Instance Method Details
74
75
76
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 74
def [](key)
super(convert_key(key))
end
|
#[]=(key, value) ⇒ Object
Also known as:
store
78
79
80
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 78
def []=(key, value)
super(convert_key(key), convert_value(value))
end
|
#assoc(key) ⇒ Object
60
61
62
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 60
def assoc(key)
super(convert_key(key))
end
|
185
186
187
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 185
def compact
dup.tap(&:compact!)
end
|
#default(*args) ⇒ Object
50
51
52
53
54
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 50
def default(*args)
args.map!(&method(:convert_key))
super(*args)
end
|
#default=(value) ⇒ Object
56
57
58
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 56
def default=(value)
super(convert_value(value))
end
|
#delete(key) ⇒ Object
102
103
104
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 102
def delete(key)
super(convert_key(key))
end
|
#dig(key, *other_keys) ⇒ Object
107
108
109
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 107
def dig(key, *other_keys)
super(convert_key(key), *other_keys)
end
|
#fetch(key, *args) ⇒ Object
68
69
70
71
72
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 68
def fetch(key, *args)
args.map!(&method(:convert_value))
super(convert_key(key), *args)
end
|
#fetch_values(*keys) ⇒ Object
111
112
113
114
115
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 111
def fetch_values(*keys)
keys.map!(&method(:convert_key))
super(*keys)
end
|
#key(value) ⇒ Object
84
85
86
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 84
def key(value)
super(convert_value(value))
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?, include?, member?
88
89
90
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 88
def key?(key)
super(convert_key(key))
end
|
#merge(*other_hashes, &block) ⇒ Object
147
148
149
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 147
def merge(*other_hashes, &block)
dup.merge!(*other_hashes, &block)
end
|
#merge!(*other_hashes) ⇒ Object
Also known as:
update
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 129
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) ⇒ Object
64
65
66
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 64
def rassoc(value)
super(convert_value(value))
end
|
#reject(*args, &block) ⇒ Object
179
180
181
182
183
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 179
def reject(*args, &block)
return to_enum(:reject) unless block_given?
dup.tap { |hash| hash.reject!(*args, &block) }
end
|
#replace(other_hash) ⇒ Object
151
152
153
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 151
def replace(other_hash)
super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end
|
#select(*args, &block) ⇒ Object
173
174
175
176
177
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 173
def select(*args, &block)
return to_enum(:select) unless block_given?
dup.tap { |hash| hash.select!(*args, &block) }
end
|
#slice(*keys) ⇒ Object
117
118
119
120
121
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 117
def slice(*keys)
keys.map!(&method(:convert_key))
self.class[super(*keys)]
end
|
164
165
166
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 164
def transform_keys(&block)
dup.transform_keys!(&block)
end
|
168
169
170
171
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 168
def transform_keys!
super
super(&method(:convert_key))
end
|
155
156
157
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 155
def transform_values(&block)
dup.transform_values!(&block)
end
|
159
160
161
162
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 159
def transform_values!
super
super(&method(:convert_value))
end
|
#value?(value) ⇒ Boolean
Also known as:
has_value?
96
97
98
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 96
def value?(value)
super(convert_value(value))
end
|
#values_at(*keys) ⇒ Object
123
124
125
126
127
|
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/indifferent_hash.rb', line 123
def values_at(*keys)
keys.map!(&method(:convert_key))
super(*keys)
end
|