{"id":7047,"date":"2014-05-17T00:24:42","date_gmt":"2014-05-17T00:24:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/problem-about-class-variables-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:24:42","modified_gmt":"2014-05-17T00:24:42","slug":"problem-about-class-variables-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/problem-about-class-variables-collection-of-common-programming-errors\/","title":{"rendered":"problem about class-variables-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/zL1re.jpg?s=32&amp;g=1\" \/><br \/>\nsawa<br \/>\nruby class-variables<br \/>\nSetting a class variable @@foo in two classes B and C, where neither is a subclass of the other but they both include a common module A, seems to create @@foo separately for B and C, which A cannot access:module A; end class B; include A; @@foo = 1 end class C; include A; @@foo = 2 endmodule A; p @@foo end # =&gt; NameError: uninitialized class variable @@foo in A class B; p @@foo end # =&gt; 1 class C; p @@foo end # =&gt; 2But when @@foo is assigned in A, which works as an ancestor to both B<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/36263400e4841d79d75ae95a852de2d9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\naabes<br \/>\nruby-on-rails class-variables<br \/>\nI&#8217;m a bit miffed why this:cattr_accessor :aggregate { true }fails with the error: syntax error, unexpected &#8216;{&#8216;, expecting keyword_endcattr_accessor :aggregate { true }^while exactly the same thing, with do\/end seems to be doing the right thing:cattr_accessor :aggregate do true end(but is whhhay too verbose ;)The source from :http:\/\/api.rubyonrails.org\/classes\/Class.html#method-i-cattr_writer is a bit too crufty given the time I have to spend on this. Thoughts?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/745b8f8ea2322d856ba5b54ff56a8022?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRafa<br \/>\nruby inheritance class-variables<br \/>\nLike the following code shows, having class accessors defined in the superclass could have unexpected behaviours because the class accessor is the same variable for all the subclasses.class Super cattr_accessor :name endclass SubA &lt; Super; end class SubB &lt; Super; endSubA.name = &#8220;A&#8221; SubB.name = &#8220;B&#8221;SubA.name=&gt; &#8220;B&#8221; # unexpected!I want to have an independent class accessor for each subclass, so a possible solution is moving the cattr_accessor from the superclass and putting it in every subc<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/01bbb59a1c3ef9960f25a97afe7e920f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAndrew Grimm<br \/>\nruby instance-variables local-variables class-variables<br \/>\nSame in Ruby 1.8.7 and 1.9.2:$ irbruby-1.8.7-p302 &gt; foo.nil? NameError: undefined local variable or method `foo&#8217; for #&lt;Object:0x3794c&gt;from (irb):1ruby-1.8.7-p302 &gt; @bar.nil?=&gt; true ruby-1.8.7-p302 &gt; @@wah.nil? NameError: uninitialized class variable @@wah in Objectfrom (irb):3why the instance variable treated differently than a local and class variable?<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n<p>I discovery a place to host code\u3001demo\u3001 blog and websites.<br \/>\nSite access is fast but not money<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.m5zn.com\/newuploads\/2014\/01\/30\/jpg\/e7da807964b1fff.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>sawa ruby class-variables Setting a class variable @@foo in two classes B and C, where neither is a subclass of the other but they both include a common module A, seems to create @@foo separately for B and C, which A cannot access:module A; end class B; include A; @@foo = 1 end class C; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7047","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7047"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7047\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}