Sha256: 188d5ed87e8a92440ca4d318aba765bdc14aab63f5169ad56a87cf1b98dfe4e7
Contents?: true
Size: 968 Bytes
Versions: 2
Compression:
Stored size: 968 Bytes
Contents
class Hash # >> {"a"=>{"b"=>{"c"=>"foo", "d"=>"bar"}, "c"=>"j"}, "q"=>"asd"}.unwind # => {"a.c"=>"j", "a.b.d"=>"bar", "q"=>"asd", "a.b.c"=>"foo"} def unwind(separator = ".", key = nil, start = {}) self.inject(start){|hash,k| expanded_key = [key, k[0]].compact.join( separator ) if k[1].is_a? Hash k[1].unwind(separator, expanded_key, hash) else hash[ expanded_key ] = k[1] end hash } end # >> {"a.b.c" => "foo", "a.b.d" => "bar", "a.c" => "j", "q" => "asd"}.wind # => {"a"=>{"b"=>{"c"=>"foo", "d"=>"bar"}, "c"=>"j"}, "q"=>"asd"} def wind(separator = ".", key = nil, start = {}) wound = Hash.new self.each {|key, value| keys = key.split( separator ) index = 0 keys.inject(wound){|h,v| index += 1 if index >= keys.size h[v.to_sym] = value break else h[v.to_sym] ||= {} end } } wound end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
theoooo-i18n-0.2.1 | lib/i18n/hash.rb |
theoooo-i18n-0.2.2 | lib/i18n/hash.rb |