Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't see a final answer to the original post. I recently read the book "Eloquent Ruby" by Russ Olsen, so decided to add my take-away here.</p> <p>The scope in which the first example defines the single-@ variable is a class-scope, i.e., if the keyword <em>self</em> were to appear outside a method def but still within the class definition, it would reference the object Document, which is in turn an instance of the Class class.</p> <p>The first example therefore defines a class instance variable, i.e., the variable @default_font is an instance variable of the object Document, a class object.</p> <p><strong>This class instance variable should only be accessed via the class level accessor methods,</strong> e.g., Document.default_font, rather than say, an_instance_of_Document.default_font</p> <p>So if you define the attr_accessors:</p> <pre><code>class Document @default_font = :Arial # this is a class instance variable, # def self.default_font # @default_font # end # # def self.default_font=(font) # @default_font = font # end # or replace the above two class methods with the following class &lt;&lt; self attr_accessor :default_font end end </code></pre> <p>Here's some irb output:</p> <pre><code>1.9.3p429 :002 &gt; require './document.rb' =&gt; true 1.9.3p429 :004 &gt; Document.default_font =&gt; :Arial 1.9.3p429 :005 &gt; Document.default_font = :Times =&gt; :Times 1.9.3p429 :006 &gt; Document.default_font =&gt; :Times 1.9.3p429 :007 &gt; doc = Document.new =&gt; #&lt;Document:0x007fe6730d8228&gt; 1.9.3p429 :008 &gt; doc.default_font NoMethodError: undefined method `default_font' for #&lt;Document:0x007fe6730d8228&gt; </code></pre> <p>The fact that the second example in the original post does not contain a single-@ variable definition outside a method def, indicates that the @default_font here is an instance variable, which can only be referenced by an instantiated object of class Document. </p> <pre><code>class Document def default_font=(font) @default_font = font # this defines and set an instance variable end def default_font @default_font # this defines an instance variable end # or above two defs can be replaced by the following attr_accessor :default_font # this defines the instance variable and access methods end </code></pre> <p>Here's some irb output:</p> <pre><code>1.9.3p429 :001 &gt; require './document.rb' =&gt; true 1.9.3p429 :002 &gt; Document.default_font NoMethodError: undefined method `default_font' for Document:Class 1.9.3p429 :003 &gt; doc = Document.new =&gt; #&lt;Document:0x007ffbf1136f88&gt; 1.9.3p429 :004 &gt; doc.default_font =&gt; nil 1.9.3p429 :005 &gt; doc.default_font = :Arial =&gt; :Arial 1.9.3p429 :006 &gt; doc.default_font =&gt; :Arial </code></pre>
    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. This table or related slice is empty.
    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