On Sat, Jan 09, 2010 at 03:57:33AM +0900, 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. 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

-- 
Aaron Patterson
http://tenderlovemaking.com/