Mayank K. wrote in post #986119:
>
> I am very new to Ruby and coming from C++ background, the class
> variables
> act differently in Ruby vs C++ , Thanks for the explanation Gary

Then here's a tip: some experts think class variables should be 
unceremoniously eliminated from the language, and that you should favor 
"class instance variables" in any case.

Here is an example of a class instance variable:

class ABC
  @x = 10

  def self.get_at_x  #class method
    @x
  end

  def get_at_x       #instance method
    @x
  end

end

puts ABC.get_at_x

obj = ABC.new
puts obj.get_at_x

--output:--
10
nil


Instance variables are attached to whatever object is self at the time, 
self is equal to the class when you are inside a class definition and 
outside of any def block, so @x gets attached to ABC.  As the output 
demonstrates, class instance variables cannot be accessed using an 
object of the class.

In addition, class instance variables are *not* inherited while class 
variables are inherited:


class ABC
  @x = 10

  @@greeting = 'hello'

  def self.get_at_x  #class method
    @x
  end


end

class XYZ < ABC

   def get_class_var
      puts @@greeting
   end

end

xyz_obj = XYZ.new
xyz_obj.get_class_var

puts XYZ.get_at_x

--output:--
hello
nil

-- 
Posted via http://www.ruby-forum.com/.