module BB::Hash
Hash
utilities.
Public Class Methods
flatten_prop_style(input = {}, opts = {}, output = {})
click to toggle source
Recursively flatten a hash to property-style format. This is a lossy conversion and should only be used for display-purposes.
@example
input = { :a => { :b => :c } } BB::Hash.flatten_prop_style(input) => {"a.b"=>:c}
@example
input = { :a => { :b => [:c, :d, :e] } } BB::Hash.flatten_prop_style(input) => {"a.b"=>"c,d,e"}
@param [Hash] input Input hash @param [Hash] opts Options @option opts [String] :delimiter
Key delimiter (Default: '.')
@param [Hash] output (leave this blank) @return [Hash] Output hash (flattened)
# File lib/blackbox/hash.rb, line 34 def flatten_prop_style(input = {}, opts = {}, output = {}) input.each do |key, value| key = opts[:prefix].nil? ? key.to_s : "#{opts[:prefix]}#{opts[:delimiter] || '.'}#{key}" case value when ::Hash flatten_prop_style(value, opts.merge(prefix: key), output) when Array output[key] = value.join(',') else output[key] = value end end output end
symbolize_keys(hash)
click to toggle source
Symbolize all top level keys.
@param [Hash] hash Input hash @return [Hash] Output hash (with symbolized keys)
# File lib/blackbox/hash.rb, line 11 def symbolize_keys(hash) hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } end