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

**URL:** <https://www.ruby-forum.com/t/not-sure-what-is-wrong-seems-to-look-right-to-me/100558>\
**Category:** Ruby\
**Created:** [July 30, 2006, 5:31am UTC](https://www.ruby-forum.com/t/not-sure-what-is-wrong-seems-to-look-right-to-me/100558 "2006-07-30T05:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 30, 2006, 5:31am UTC](https://www.ruby-forum.com/t/not-sure-what-is-wrong-seems-to-look-right-to-me/100558/1 "2006-07-30T05:31:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 30, 2006, 5:59am UTC](https://www.ruby-forum.com/t/not-sure-what-is-wrong-seems-to-look-right-to-me/100558/2 "2006-07-30T05:59:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 30, 2006, 6:14am UTC](https://www.ruby-forum.com/t/not-sure-what-is-wrong-seems-to-look-right-to-me/100558/3 "2006-07-30T06:14:55Z")

</div>

Ben A. 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
