From: 7stud -- Date: 2009-07-23T10:15:42+09:00 Subject: Re: why I get an empty hash? Li Chen wrote: > If what you explain is right, how come I get 'AA' when I add a line > here: > def table(array,word_size=1,word='') > hash_table=Hash.new(0) > > array.each do |e| > > if word_size==1 > puts "#{word+e}"############# add a line to see if 'AA' is > created > hash_table["#{word+e}" ]=0 > > else > table(array,word_size-1,word+e ) > end > end > Whoops, I didn't see that the recursive call to table() uses word+e as the last argument. So let's make the changes: (2)table(array=["A"], word_size=1, word='A') #Execution halts in the first table call until #the return value of this recursive table() call can #be determined... (2) table(array=["A"], word_size=1, word='A') -------------------- hash_table = {} e = "A" if true hash_table["AA"] = 0 (1) ------------ =begin hash_table = {} e = "A" if false =end else {"AA" => 0} #from (2) return hash_table So you see, the result is the same. It doesn't matter what you do to the hash returned by the recursive method calls because you don't do anyting with the returned hash, so ruby discards it. > I still don't figure how to return the hash I want. How about returning that {"AA" => 0} hash? -- Posted via http://www.ruby-forum.com/.