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
39 def self.[](*args)
40   new.merge!(Hash[*args])
41 end
new(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
43 def initialize(*args)
44   super(*args.map(&method(:convert_value)))
45 end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
67 def [](key)
68   super(convert_key(key))
69 end
[]=(key, value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
71 def []=(key, value)
72   super(convert_key(key), convert_value(value))
73 end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
55 def assoc(key)
56   super(convert_key(key))
57 end
default(*args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
47 def default(*args)
48   super(*args.map(&method(:convert_key)))
49 end
default=(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
51 def default=(value)
52   super(convert_value(value))
53 end
delete(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
95 def delete(key)
96   super(convert_key(key))
97 end
dig(key, *other_keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
 99 def dig(key, *other_keys)
100   super(convert_key(key), *other_keys)
101 end
fetch(key, *args) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
63 def fetch(key, *args)
64   super(convert_key(key), *args.map(&method(:convert_value)))
65 end
fetch_values(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
103 def fetch_values(*keys)
104   super(*keys.map(&method(:convert_key)))
105 end
has_key?(key)
Alias for: key?
has_value?(value)
Alias for: value?
include?(key)
Alias for: key?
key(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
77 def key(value)
78   super(convert_value(value))
79 end
key?(key) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
81 def key?(key)
82   super(convert_key(key))
83 end
Also aliased as: has_key?, include?, member?
member?(key)
Alias for: key?
merge(other_hash, &block) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
130 def merge(other_hash, &block)
131   dup.merge!(other_hash, &block)
132 end
merge!(other_hash) { |key, self, value| ... } click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
116 def merge!(other_hash)
117   return super if other_hash.is_a?(self.class)
118 
119   other_hash.each_pair do |key, value|
120     key = convert_key(key)
121     value = yield(key, self[key], value) if block_given? && key?(key)
122     self[key] = convert_value(value)
123   end
124 
125   self
126 end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
59 def rassoc(value)
60   super(convert_value(value))
61 end
replace(other_hash) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
134 def replace(other_hash)
135   super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
136 end
slice(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
107 def slice(*keys)
108   keys.map!(&method(:convert_key))
109   self.class[super(*keys)]
110 end
store(key, value)
Alias for: []=
update(other_hash)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
   # File lib/sinatra/indifferent_hash.rb
89 def value?(value)
90   super(convert_value(value))
91 end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
    # File lib/sinatra/indifferent_hash.rb
112 def values_at(*keys)
113   super(*keys.map(&method(:convert_key)))
114 end

Private Instance Methods

convert_key(key) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
140 def convert_key(key)
141   key.is_a?(Symbol) ? key.to_s : key
142 end
convert_value(value) click to toggle source
    # File lib/sinatra/indifferent_hash.rb
144 def convert_value(value)
145   case value
146   when Hash
147     value.is_a?(self.class) ? value : self.class[value]
148   when Array
149     value.map(&method(:convert_value))
150   else
151     value
152   end
153 end