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
Li Chen wrote:
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
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
7stud – wrote:
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
Li Chen wrote:
7stud – wrote:
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########
ruby table2.rb
AA=>0
…
TG=>0
{}
Exit code: 0
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?
7stud – wrote:
7stud – wrote:
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.
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
7stud – wrote:
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.
7stud – wrote:
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
Li Chen wrote:
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.