Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/sruby/mixin.rb
Instance Method Summary collapse
-
#keys(hash = self) ⇒ Array<String>
Get the deepest keys.
-
#path_to(key, path = [], hash: self) ⇒ NilClass, Array<String, String>
Get the deep path of a certain key.
- #paths(hash = self) ⇒ Object
- #stringify_keys! ⇒ Object
- #stringify_values! ⇒ Object
Instance Method Details
#keys(hash = self) ⇒ Array<String>
Get the deepest keys
15 16 17 18 19 20 21 22 23 |
# File 'lib/sruby/mixin.rb', line 15 def keys(hash = self) hash.each_with_object([]) do |(key, value), paths| if value.is_a?(Hash) paths.concat(keys(value)) else paths << key end end end |
#path_to(key, path = [], hash: self) ⇒ NilClass, Array<String, String>
Get the deep path of a certain key
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sruby/mixin.rb', line 29 def path_to(key, path = [], hash: self) hash.stringify_keys! hash.each_pair do |k, v| return [path + [k], v] if k == key.to_s if v.is_a?(Hash) && (p = path_to(key.to_s, path + [k], :hash => v)) return p end end nil end |
#paths(hash = self) ⇒ Object
41 42 43 |
# File 'lib/sruby/mixin.rb', line 41 def paths(hash = self) keys(hash).map { |key| path_to(key, :hash => hash) } end |
#stringify_keys! ⇒ Object
4 5 6 |
# File 'lib/sruby/mixin.rb', line 4 def stringify_keys! transform_keys!(&:to_s) end |
#stringify_values! ⇒ Object
8 9 10 |
# File 'lib/sruby/mixin.rb', line 8 def stringify_values! transform_values!(&:to_s) end |