not sure what is wrong. seems to look right to me

J

junkone

class ReportBuilder
countExch=Hash.new

def getStats(symbolObjColl)
startCounter=0
#### add a loop to count thro symbolcollection.
symbolObjColl.each{|eachSymbolObj|


if (countExch.has_key?(eachSymbolObj.exchange))

countExch[eachSymbolObj.exchange]=countExch[eachSymbolObj.exchange]+=1

else
countExch[eachSymbolObj.exchange]=startCounter+=1;
end
}

puts countExch.to_s



end


end


I get the folliwng error.
1) Error:
Test_ReportBuilder#test_Case1:
NameError: undefined local variable or method `countExch' for
#<ReportBuilder:0xd29af88>


I am not sure what is wrong.

Seede
 
H

Hal Fulton

Ben said:
Instance variables are prefixed with an @ symbol. I would guess that you
want countExch to be an instance variable. Also, I think the declaration
might need to go in the initialize method.

Someone who is more experienced w/ Ruby please give a more accurate and/or
complete answer.

Basically right.

I'll point out that "camelCase" is not usually our style. We're more
likely to say "count_exch" -- of course, Ruby itself doesn't care.

I'm not sure what the code is doing really. But these statements
are very confusing at the least:

@countExch[eachSymbolObj.exchange]=@countExch[eachSymbolObj.exchange]+=1
@countExch[eachSymbolObj.exchange]=startCounter+=1;

The first is the same as saying:

@countExch[eachSymbolObj.exchange] += 1

if I'm not mistaken. And the second is more clearly represented as

startCounter += 1
@countExch[eachSymbolObj.exchange] = startCounter

But maybe you want to use each_with_index instead of each, and you
won't have to manually keep a counter.

What exactly is this all about?


Hal
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top