# Something strange with CLASS and SCOPE

**URL:** <https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287>\
**Category:** Ruby\
**Created:** [January 8, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287 "2010-01-08T19:57:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 8, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287/1 "2010-01-08T19:57:30Z")

</div>

There is onething regarding class and scope I felt puzzled for a while.  
Below is my code.

class QQ  
@strange=Array.new  
def report  
puts @strange.class  
end  
end

q=QQ.new  
q.report

From my understanding, instance variable should be available to every  
method of a specific instance. However, if I initialize the “@strange”  
array in the very begining of my class. In my method, I will derive a  
NilClass. That means, there is something to do with scope. It seems that  
every block would produce a local scope.

So, how can I overcome such deliemma. I want an Array that can be used  
by every method in the class. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 8, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287/2 "2010-01-08T20:18:41Z")

</div>

On Sat, Jan 09, 2010 at 03:57:33AM +0900, Tony T. wrote:

> q=QQ.new  
> q.report
> 
> From my understanding, instance variable should be available to every  
> method of a specific instance. However, if I initialize the “@strange”  
> array in the very begining of my class. In my method, I will derive a  
> NilClass. That means, there is something to do with scope. It seems that  
> every block would produce a local scope.

The array you’re setting is an instance variable on the Class instance  
called QQ. You want an instance variable for each new instance of QQ.

> So, how can I overcome such deliemma. I want an Array that can be used  
> by every method in the class. Thank you in advance.

```
class QQ
  def initialize
    @strange=Array.new
  end

  def report
    puts @strange.class
  end
end

q=QQ.new
q.report
```

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 8, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287/3 "2010-01-08T20:26:53Z")

</div>

On 08.01.2010 19:57, Tony T. wrote:

> q=QQ.new  
> q.report
> 
> From my understanding, instance variable should be available to every  
> method of a specific instance

Only if your instance variables have been initialized, like so:

class QQ  
def initialize # Used by Ruby when you do “QQ.new”  
@array = Array.new  
end

def report  
puts @array.class  
end  
end

q = QQ.new  
q.report

(Actually, the class instance gets initialized, and not the instance  
variables, strictly speaking.)

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 8, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/something-strange-with-class-and-scope/181287/4 "2010-01-08T21:53:32Z")

</div>

Phillip G. wrote:

> (Actually, the class instance gets initialized, and not the instance  
> variables, strictly speaking.)

It’s worth mentioning that a class is itself an object (an instance of  
class Class), and therefore has its own instance variables.

class QQ

# at this point, the current object (‘self’) is class QQ itself

@strange=Array.new

def report  
# but here, the current object is an instance of class QQ  
# so this is an instance variable in a different object  
puts @strange.class  
end  
end

Sometimes the class itself is a useful place to store values:

class QQ  
@count = 0

def self.count  
@count  
end

def self.count=(x)  
@count = x  
end

def initialize  
self.class.count = self.class.count + 1  
end  
end

a = QQ.new  
b = QQ.new  
puts “You have created #{QQ.count} QQ’s”
