variables - Ruby: @@var or @var in Ruby modules? -


is there reason not use @var or @@var in ruby module not destined used mixin in class? i've experimented , both variables results in same behavior (simple module getter , setter @var or @@var). havent found example or explanation says module can used mutable state. seems serve singleton in java.

i found piece of code used @@vars on , never include/extend -ed in other class developer using module hold state may updated want consumers of module see same state @ given time , suppose see cases valuable, since i've never seen documented cases of this, found odd.

regardless, there difference in instance @var vs 'class' @@var in context of module?

edit:

so experimented bit more. above question assuming no usage of mixins. says there class variable all.....one instance of module (again singleton) or have instance variable shared among all...one instance of module using either state results in same behavior. however, made instance method that's instance getter go along module method getter , did mixin class. in case of @@var, single module instance , classes share value set to. @var, single 'module instance' had it's own value , each class instance sets @var mixin instance method had it's own @var.

i suppose makes sense.

i don't think there difference if module not included in class. differences between instance variable of class , class variables are:

  • class variables shared among subclass. if a subclass of b, @@foo of a same object @@foo of b, @foo of a different @foo of b.
  • class variables shared among class , of instances. if a, b instances of a, @@foo of a same object @@foo of a , @@foo of b, @foo of a, @foo of a, , @foo of b diffferent.

both of these not relevant modules not included in class.

but since use of class variable may imply there such sharing between subclass and/or between class and/or instances, clarity, may better use instance variable of class rather class variable.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -