1

I guess I'm asking for the difference between @store and @@store in the following:

class Test
  @@store = 9 
  class << self
    def set_store(v)
      @store = v
    end
    def store
      @store
    end
    def sstore
      @@store
    end
  end
end

Test.set_store 8
p Test.store # 8 
p Test.sstore # 9

a = Test.new

p a.class.store # 8
p a.class.sstore # 9

Where are static variables attached to if not the eigenclass? Are the two effectively the same in terms of interaction?

Cenoc
  • 11,172
  • 21
  • 58
  • 92
  • 1
    @@variables are shared within class hierarchy (and class instances), class-level @variables are not. – Sergio Tulentsev Jul 08 '16 at 13:23
  • @SergioTulentsev I guess the https://stackoverflow.com/q/21122691/322020 lacks your comment/answer because there people advise two different things not explaining how to choose between. – Nakilon Dec 14 '22 at 20:49
  • Also mentioned in https://stackoverflow.com/a/5890199/322020 – Nakilon Dec 14 '22 at 21:07
  • 1
    @Nakilon: haha, I'm flattered that you think I'm some kind of expert on class variables. :) I've looked through the posts you linked and they look okay-ish to me. Not sure a full answer is needed there. But feel free to post this clarification yourself! – Sergio Tulentsev Dec 15 '22 at 12:27

1 Answers1

0

They are both class variables but in an instance you can access only @@store variable.

Mind that class variables are not thread safe in Ruby, so use Mutex if you plan to use varaible as a Hash.

Using class instance variable for mutex in Ruby

Community
  • 1
  • 1
Dino Reic
  • 3,586
  • 3
  • 22
  • 11