Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/hash/to_2d_hash.rb
Instance Method Summary collapse
-
#to_2d_hash(opts = {}) ⇒ Hash
Create a two-dimensional copy of the current hash.
Instance Method Details
#to_2d_hash(opts = {}) ⇒ Hash
Create a two-dimensional copy of the current hash
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/hash/to_2d_hash.rb', line 8 def to_2d_hash(opts={}) output = self.class.new self.each do |k, v| key = opts[:prefix] ? "#{opts[:prefix]}#{opts[:delimiter]||'_'}#{k}" : k if v.is_a? Hash output.merge! v.to_2d_hash(opts.merge(prefix: key)) else output[key] = v end end output end |