why I get an empty hash?

L

Li Chen

Hi all,

I modify James' code and want to return a hash with all values set to
0. But I only get an empty hash. I wonder what is wrong with my codes.

Thanks,

Li

def table(array,word_size=1,word='')
hash_table=Hash.new(0)

array.each do |e|

if word_size==1
hash_table["#{word+e}" ]=0
else
table(array,word_size-1,word+e )
end
end
return hash_table
end

######################
array=['A','C','G','T']
p table(array,2)



### here is ressults:###
 
7

7stud --

Li said:
Hi all,

I modify James' code and want to return a hash with all values set to
0. But I only get an empty hash. I wonder what is wrong with my codes.

Thanks,

Li

def table(array,word_size=1,word='')
hash_table=Hash.new(0)

array.each do |e|

if word_size==1
hash_table["#{word+e}" ]=0
else
table(array,word_size-1,word+e )
end
end
return hash_table
end

######################
array=['A','C','G','T']
p table(array,2)



### here is ressults:###
ruby table2.rb {}
Exit code: 0

Method calls are replaced in your code by the method's return value.

The recursive calls to table() are replaced with something. What? A
hash with various keys and values. But your code essentially does this:

hash_table = {}

if false
#
else
{"A" => 0}
end

puts hash_table
 
7

7stud --

Look at what happens when the array has one element:

data = ["A"]
result = table(data, 2)


(1)
table(array=["A"], word_size=2, word='')
---------------------
hash_table = {}
e = "A"

if false
else
(2)table(array=["A"], word_size=1, word='')

#Execution halts in the first table call until
#the return value of this recursive table() call can
#be determined...


(2)
table(table(array=["A"], word_size=1, word='')
--------------------
hash_table = {}
e = "A"

if true
hash_table["A"] = 0

return {"A" => 0}

#Now the return value for the recursive table() call has been
#determined, so the return value can be substituted in (1) and
#execution can continue...


(1)
------------
=begin
hash_table = {}
e = "A"

if false
=end

else
{"A" => 0} #from (2)

return hash_table
 
L

Li Chen

7stud said:
Look at what happens when the array has one element:

data = ["A"]
result = table(data, 2)

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

return hash_table
end
 
L

Li Chen

Li said:
7stud said:
Look at what happens when the array has one element:

data = ["A"]
result = table(data, 2)

Hi,

Given that I can print out the hash but the return one in main program
is empty, I still don't figure how to return the hash I want.


Li


def table(array,word_size=1,word='')

hash_table=Hash.new(0)
array.each do |e|
if word_size==1
word+ e
hash_table[word+e]=0
else
table(array,word_size-1,word+e )
end
end
hash_table.each {|k,v|print k, "=>",v,"\n"}# it looks like I have a
hash here
# and I can print out the
results
end

######################
array=['A','C','G','T']

p table(array,2).each {|k,v|print k, "=>",v,"\n"}


######output results########
 
7

7stud --

Li said:
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?
 
7

7stud --

7stud said:
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.

Your question is like asking why the result of calling this method:

def meth
h = {}
{"AA" => 0}

return h
end

p meth


is an empty hash.
 
7

7stud --

7stud said:
Your question is like asking why the result of calling this method:

def meth
h = {}
{"AA" => 0}

return h
end

p meth


is an empty hash.

Which is equivalent to asking why the result of calling meth1 below is
an empty hash:

def meth1
h = {}
meth2

return h
end

def meth2
h = {"AA" => 0}
return h
end

p meth1


Method calls are replaced in your code by the return value of the
method. So calling meth1 results in this:


h = {}
{'AA' => 0} #from meth2

return h
 
L

Li Chen

7stud said:
Method calls are replaced in your code by the return value of the
method. So calling meth1 results in this:


h = {}
{'AA' => 0} #from meth2

return h

Hi,

I think I now understand why I can't get expected hash:
I do nothing about the value of method call after it returns. Here is
the line that returns the hash:

...
h={}
temp_hash=method2
h.merge(tem_hash)
return h

Thanks,

Li
 
7

7stud --

Li said:
I think I now understand why I can't get expected hash:
I do nothing about the value of method call after it returns. Here is
the line that returns the hash:

...
h={}
temp_hash=method2
h.merge(tem_hash)
return h

Those 4 lines are equivalent to:

return method2

In ruby, that line looks strange--it looks like you are trying to return
the method itself. However, that line is equivalent to:

return method2()

and the call to method2() will be replaced by the return value of
method2.
 
L

Li Chen

7stud said:
Those 4 lines are equivalent to:

return method2

In ruby, that line looks strange--it looks like you are trying to return
the method itself. However, that line is equivalent to:

return method2()

and the call to method2() will be replaced by the return value of
method2.

Thank you so much,

Li
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top