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
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.
···
From my understanding, instance variable should be available to every
--
Posted via http://www.ruby-forum.com/.
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
···
On Sat, Jan 09, 2010 at 03:57:33AM +0900, Tony Tony wrote:
--
Aaron Patterson
http://tenderlovemaking.com/
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.)
···
On 08.01.2010 19:57, Tony Tony wrote:
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
--
Phillip Gawlowski
Phillip Gawlowski 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"
···
--
Posted via http://www.ruby-forum.com/\.