Inversely, Hash#except
returns a new hash excluding the specified keys:
options = {:a => 3, :b => 4, :c => 5}
options.slice(:a, :c) # => {:c=>5, :a=>3}
options.except(:a) # => {:b=>4, :c=>5}
Hash#slice and Hash#except also come in in-place versions, respectively: Hash#slice!
and Hash#except!.
HashWithIndifferentAccess core_ext/hash/indifferent_access.rb
A HashWithIndifferentAccess is a hash whose elements can be accessed with either string
or symbol keys:
options = {:a => 3, :b => 4}.with_indifferent_access
options.class # => HashWithIndifferentAccess
options[:a] # => 3
options["a"] = 100
options[:a] # => 100
HashWithIndifferentAccess is primarily used to offer a nice API to users, for example, so
that a developer can write params[:user] or params["user"]. The stringify_keys or
symbolize_keys methods should be used in option processing.
However, HashWithIndifferentAccess also fixes a class of security exploits in Rails. In Ruby
1.8, symbols are not garbage collected, so a params hash purely keyed on symbols (as it
used to be) could lead to a denial-of-service attack. A malicious client could leak memory
and exhaust the symbol table by issuing many requests with unique parameter names. Incidentally,
Ruby 1.9 will obviate the need for HashWithIndifferentAccess, because symbols
and strings will be treated identically (:aoeu == 'aoeu').
Kernel
Integer | 69
Integer core_ext/integer/even_odd.
Pages:
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113