简体   繁体   中英

In Ruby, how can I convert this string format to a hash

The string format is like this

 "a.b.c = 1"
 "a.b.d = 2"
=> its hash will be
=> {'a'=> {'b' => {'c'=>1, 'd'=>2 } } }

It gets tricky with arrays

 "a.e[0].f = 1"
 "a.e[0].g = 2"
 "a.e[1].h = 3"
 => its hash will be
 => {'a' => {'e' => [{'f'=>1, 'g'=>2}, {'h'=>3}] } }

I wrote a version which doesn't handle arrays with too many if-else checks

def construct
  $output[$words[0]] = {} unless $output.has_key?($words[0])
  pointer = $output[$words[0]]
  $words[1..-2].each do |word|
    pointer[word] = {} unless pointer.has_key?(word)
    pointer = pointer[word]
  end
  pointer[$words[-1]] = 1
end

$output = {}
$words = ['a', 'b', 'c']
construct
$words = ['a', 'b', 'd']
construct
p $output

The array version is even worse. Is there a better way of solving this in Ruby?

Not as simple as I did expect. Here is what I came up with:

class Hash
  def hash_merge(other)
    update(other) do | key, val_self, val_other |
      case val_self
      when Hash
        val_self.hash_merge val_other
      when Array
        val_self += [nil] * (val_other.length - val_self.length)
        val_self.zip(val_other).map { | a, b | a && b ? a.hash_merge(b) : a || b }
      else
        # error
      end
    end
  end
end

# parses a string of the form "a.e[1].g = 2" to a hash {"a" => {"e" => [nil , {"g" => 2}]}}
def construct_string(s)
  if s =~ /\A\s*(\S+)\s+=\s+(\d+)\z/
    s1, s2 = $1, $2
    s1.split('.').reverse.inject(s2.to_i) do | hash, name |
      if name =~ /\[(\d+)\]\z/
        name, idx = $~.pre_match, $1.to_i
        array = []
        array[idx] = hash
        { name => array }
      else
        { name => hash }
      end
    end
  else 
    # error
  end
end

# parses an array of string and merges the resulting hashes
def construct(s)
  s.map { | e | construct_string(e) }.inject(&:hash_merge)
end

ins = ["a.e[0].f = 1",
       "a.e[0].g = 2",
       "a.e[1].h = 3"]

p construct(ins)   # {"a"=>{"e"=>[{"f"=>1, "g"=>2}, {"h"=>3}]}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM