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

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

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.

Ben

class ReportBuilder
def initialize
   @countExch=Hash.new
end

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

···

--
Ben Atkin
ben@benatkin.com
http://www.benatkin.com/
(928) 380-0656

Ben Atkin wrote:

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