Ruby - Write program to show the difference between instance variable and class variable

Requirements

Write program to show the difference between instance variable and class variable

Demo

class MyClass 
   @@classvar = 1000 # from ww w  .  j  a v  a 2 s .  c  o  m
   @instvar = 1000 
                        
   def MyClass.classMethod 
      if @instvar == nil then 
         @instvar = 10 
      else 
         @instvar += 10 
      end 
   end 
                        
   def instanceMethod 
      if @instvar == nil then 
         @instvar = 1 
      else 
         @instvar += 1 
      end             
   end          
end 

ob = MyClass.new 
puts MyClass.instance_variable_get(:@instvar) 
puts( '--------------' ) 
for i in 0..2 do    
   # MyClass.classMethod 
   ob.instanceMethod 
   puts( "MyClass @instvar=#{MyClass.instance_variable_get(:@instvar)}") 
   puts( "ob @instvar= #{ob.instance_variable_get(:@instvar)}" ) 
end

Result

Here, you create a single instance (ob) at the outset.

When the ob.instanceMethod is called, @instvar is incremented by 1.

Related Exercise