Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A variable prefixed with <code>@</code> is an <em>instance variable</em>, while one prefixed with <code>@@</code> is a <em>class variable</em>. Check out the following example; its output is in the comments at the end of the <code>puts</code> lines:</p> <pre><code>class Test @@shared = 1 def value @@shared end def value=(value) @@shared = value end end class AnotherTest &lt; Test; end t = Test.new puts "t.value is #{t.value}" # 1 t.value = 2 puts "t.value is #{t.value}" # 2 x = Test.new puts "x.value is #{x.value}" # 2 a = AnotherTest.new puts "a.value is #{a.value}" # 2 a.value = 3 puts "a.value is #{a.value}" # 3 puts "t.value is #{t.value}" # 3 puts "x.value is #{x.value}" # 3 </code></pre> <p>You can see that <code>@@shared</code> is shared between the classes; setting the value in an instance of one changes the value for all other instances of that class and even child classes, where a variable named <code>@shared</code>, with one <code>@</code>, would not be.</p> <p><strong>[Update]</strong></p> <p>As Phrogz mentions in the comments, it's a common idiom in Ruby to track class-level data with an instance variable <em>on the class itself</em>. This can be a tricky subject to wrap your mind around, and there is plenty of <a href="http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/">additional reading</a> on the subject, but think about it as modifying the <code>Class</code> class, but <em>only</em> the instance of the <code>Class</code> class you're working with. An example:</p> <pre><code>class Polygon class &lt;&lt; self attr_accessor :sides end end class Triangle &lt; Polygon @sides = 3 end class Rectangle &lt; Polygon @sides = 4 end class Square &lt; Rectangle end class Hexagon &lt; Polygon @sides = 6 end puts "Triangle.sides: #{Triangle.sides.inspect}" # 3 puts "Rectangle.sides: #{Rectangle.sides.inspect}" # 4 puts "Square.sides: #{Square.sides.inspect}" # nil puts "Hexagon.sides: #{Hexagon.sides.inspect}" # 6 </code></pre> <p>I included the <code>Square</code> example (which outputs <code>nil</code>) to demonstrate that this may not behave 100% as you expect; the <a href="http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/">article I linked above</a> has plenty of additional information on the subject.</p> <p>Also keep in mind that, as with most data, you should be extremely careful with class variables in a <a href="http://m.onkey.org/thread-safety-for-your-rails">multithreaded environment</a>, as per dmarkow's comment.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload