So, I have this ruby ββcode, which I grabbed from Wikipedia, and changed a bit:
@trie = Hash.new() def build(str) node = @trie str.each_char { |ch| cur = ch prev_node = node node = node[cur] if node == nil prev_node[cur] = Hash.new() node = prev_node[cur] end } end build('dogs') puts @trie.inspect
I first ran this on the irb console, and every time I output node , it just left me an empty hash every time {} , but when I really call this assembly of functions using the 'dogs' parameter line, it actually works. and outputs {"d"=>{"o"=>{"g"=>{"s"=>{}}}}} , which is absolutely correct.
This is probably a Ruby question rather than a real question about how the algorithm works. I do not have enough Ruby knowledge to decrypt what is going on there, I think.
source share