-2

Given this example:

class Pepe
   def self.test
     # this is a instance variable of a class
     @klass_var = 123
   end

   def instance_method
     # here self is an instance of Pepe, @@klass_var is the same than previous @klass_var ?
     @@klass_var
   end
end

Why @@klass_var is not the same?

Arnold Roa
  • 7,335
  • 5
  • 50
  • 69
  • 1
    Possible duplicate of [Ruby class instance variable vs. class variable](https://stackoverflow.com/questions/15773552/ruby-class-instance-variable-vs-class-variable) – Silvio Mayolo Nov 04 '17 at 01:01

1 Answers1

-3
class Pepe
   @@klass_var = 345
   def self.test
     # this is a instance variable of a class
     @@klass_var = 123
   end

   def instance_method
     # here self is an instance of Pepe, @@klass_var is the same than previous @klass_var ?
     @@klass_var
   end
end

Pepe.new.instance_method # 345

Pepe.test # this sets @@klass_var

Pepe.new.instance_method # 123
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69