Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a method to this madness...</p> <pre><code>class Example @foo # class instance variable @@bar # class variable def fun1 @baz # instance variable end end </code></pre> <h2>Instance variables</h2> <p>Instance variables (<code>@foo</code> and <code>@baz</code> in the example) always begin with <code>@</code>, and <strong>they always <em>belong to</em> whatever object self refers to</strong>: either an object of the class, or the Class object representing a class. An instance variable reference in a class definition or class method is completely different from an instance variable reference in an instance method.</p> <p><strong>Inheritance</strong><br/> Because instance variables are not defined by a class, they are unrelated to the inheritance mechanism —they are simply created when a value is assigned to them. Class instance variables, being simply instance variables of the Class object that represents a class, are thus not inherited. </p> <h2>Class variables</h2> <p>Class variables are visible to, and shared by, the class methods and the instance methods of a class, and also by the class definition itself. Class variables can be used in instance methods, class methods, and in the class definition itself, outside of any method. <strong>Class variables are always evaluated in reference to the class object created by the enclosing class definition statement</strong>.</p> <h2>Class instance variable vs instance variable</h2> <p>A disadvantage of class instance variables is that they cannot be used within instance methods as class variables can. Another disadvantage is the potential for confusing them with ordinary instance variables. An advantage of class instance variables over class variables has to do with the confusing behavior of class variables when subclassing an existing class: if a class uses class variables, then any subclass can alter the behavior of the class and all its descendants by changing the value of the shared class variable. This is a strong argument for the use of class instance variables instead of class variables.</p> <p>Much of this is from the excellent <a href="http://rads.stackoverflow.com/amzn/click/0596516177" rel="nofollow noreferrer">"The Ruby Programming Language"</a><br/> <a href="http://ecx.images-amazon.com/images/I/517LDwIEYwL._SL75_.jpg" rel="nofollow noreferrer">alt text http://ecx.images-amazon.com/images/I/517LDwIEYwL._SL75_.jpg</a></p>
    singulars
    1. This table or related slice is empty.
    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. 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