Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#squash(separator = " ") ⇒ Object

Converts a deeply nested hash of stringish keys and values to an array of flattened branches

For example, consider:

hash = {
  :with => {
    :b => [1, 2]
  },
  :without => "c"
}

hash.squash("_") =>
  [
    "with_b_1",
    "with_b_2",
    "without_c"
  ]

This is useful for readable hash options, like:

magic :with => [ "Jack", "Giant" => { :in => "sky", :on => [ "ground", "harp" ] } ]


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'ext/hash.rb', line 26

def squash(separator=" ")
  inject([]) do |memo, (k, v)|
    key = if k.respond_to?(:squash) then k.squash else [ k.to_s ] end
    val = if v.respond_to?(:squash) then v.squash else [ v.to_s ] end

    # Loop over all combos
    for sk in key do
      for sv in val do
        memo << (sk + separator + sv)
      end
    end

    memo
  end

end