Ruby 类的实例变量是对象的类变量吗?
举个例子:Ruby 类的实例变量是对象的类变量吗?,ruby,Ruby,举个例子: 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 e
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
为什么@@klass\u var
不一样?可能重复
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