Sha256: 1593f625c3647d360713fe0282bb3c2b4c13a47ccf7791f78973b14ec3efe3c4
Contents?: true
Size: 779 Bytes
Versions: 5
Compression:
Stored size: 779 Bytes
Contents
################################################################################ # Extend fast_trie gem with the ability to put the trie into our hash format. ################################################################################ class Trie # Convert the Trie to Hash. def to_h { "_" => self.to_h_impl(self.root) } end # Recursive implementation function for `to_h`. def to_h_impl(root) result = {} ('a'..'z').each do |i| if root.walk(i) result[i] = {} unless result.key?(i) if root.walk(i).terminal? result[i]['$'] = root.walk(i).value end unless root.walk(i).leaf? result[i]['_'] = self.to_h_impl(root.walk(i)) end end end result end end # class Trie
Version data entries
5 entries across 5 versions & 1 rubygems