something strange with CLASS and SCOPE

T

Tony Tony

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

Aaron Patterson

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.

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
 
P

Phillip Gawlowski

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

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.)
 
B

Brian Candler

Phillip said:
(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"
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top