Static variables / instances in ruby

I am new to Ruby. You have a very simple question about static and instance variables.

class Test def self.init @@var_static = 1 @member = 2 end def self.print puts "@@var_static: #{@@var_static}" puts "@member: #{@member}" end end Test.init Test.print 

Why does the above code initialize the member variable: @member, inside the static method: Test :: init? My understanding was that using @member will throw an error because it is not bound to any instance of the Test class. But the error does not occur.

+4
source share
2 answers

A class is an instance of an object, although it can have instance variables, like any other object:

 >> Fixnum.class => Class 

Fixnum class is an instance of Class !

+3
source

All Articles