Class: Hash
Overview
Copyright 2010-2014 Tasos Laskos <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Direct Known Subclasses
Instance Method Summary collapse
-
#downcase ⇒ Hash
Self with the keys and values converted to lower-case strings.
-
#find_symbol_keys_recursively ⇒ Array<Symbol>
Returns all symbol keys from
self
and children hashes. -
#recode ⇒ Hash
Recursively converts the hash’s string data to UTF8.
-
#stringify_keys(recursively = true) ⇒ Hash
Converts the hash keys to strings.
-
#symbolize_keys(recursively = true) ⇒ Hash
Converts the hash keys to symbols.
Instance Method Details
#downcase ⇒ Hash
Returns Self with the keys and values converted to lower-case strings.
49 50 51 52 53 54 55 56 |
# File 'lib/arachni/ruby/hash.rb', line 49 def downcase stringify_keys.inject({}) do |h, (k, v)| k = k.downcase if k.is_a?( String ) v = v.downcase if v.is_a?( String ) h[k] = v h end end |
#find_symbol_keys_recursively ⇒ Array<Symbol>
Returns all symbol keys from self
and children hashes.
72 73 74 75 76 77 78 79 |
# File 'lib/arachni/ruby/hash.rb', line 72 def find_symbol_keys_recursively flat = [] each do |k, v| flat << k flat |= v.find_symbol_keys_recursively if v.is_a?( Hash ) &&v.any? end flat.reject { |i| !i.is_a? Symbol } end |
#recode ⇒ Hash
Recursively converts the hash’s string data to UTF8.
62 63 64 65 66 67 68 |
# File 'lib/arachni/ruby/hash.rb', line 62 def recode recoded = {} each do |k, v| recoded[k] = (v.respond_to?( :recode ) ? v.recode : v) end recoded end |
#stringify_keys(recursively = true) ⇒ Hash
Converts the hash keys to strings.
25 26 27 28 29 30 31 |
# File 'lib/arachni/ruby/hash.rb', line 25 def stringify_keys( recursively = true ) stringified = {} each do |k, v| stringified[k.to_s] = (recursively && v.is_a?( Hash ) ? v.stringify_keys : v) end stringified end |
#symbolize_keys(recursively = true) ⇒ Hash
Converts the hash keys to symbols.
39 40 41 42 43 44 45 |
# File 'lib/arachni/ruby/hash.rb', line 39 def symbolize_keys( recursively = true ) symbolize = {} each do |k, v| symbolize[k.to_s.to_sym] = (recursively && v.is_a?( Hash ) ? v.symbolize_keys : v) end symbolize end |