Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>self</code> references the current object. This lends itself to many uses:</p> <h1>calling a method on the current object</h1> <pre><code>class A def initialize val @val = val end def method1 1 + self.method2() end def method2 @val*2 end end </code></pre> <p>Here running <code>A.new(1).method1()</code> will return <code>3</code>. The use of <code>self</code> is optional here - the following code is equivalent:</p> <pre><code>class A def initialize val @val = val end def method1 1 + method2() end def method2 @val*2 end end </code></pre> <p><code>self</code> is not redundant for this purpose though - operator overloading makes it neccessary:</p> <pre><code>class A def initialize val @val = val end def [] x @val + x end def method1 y [y] #returns an array! end def method2 y self.[y] #executes the [] method end end </code></pre> <p>This shows how self must be used if you want to call the current object's <code>[]</code> method.</p> <h1>referencing attributes</h1> <p>You can generate the methods to read and write to instance variables using <code>attr_accessor</code> and co. </p> <pre><code>class A attr_accessor :val def initialize val @val = val end def increment! self.val += 1 end end </code></pre> <p>Using self <em>is</em> redundant here because you can just reference the variable directly, eg. <code>@val</code>. Using the previous class, <code>A.new(1).increment!</code> would return 2.</p> <h1>method chaining</h1> <p>You can return self to provide a form of syntactical sugar known as chaining:</p> <pre><code>class A attr_reader :val def initialize val @val = val end def increment! @val += 1 self end end </code></pre> <p>Here, because we are returning the current object, methods can be chained:</p> <pre><code>A.new(1).increment!.increment!.increment!.val #returns 4 </code></pre> <h1>creating class methods</h1> <p>You can define class methods using self:</p> <pre><code>class A def self.double x x*2 end def self.quadruple x self.double(self.double(x)) end end </code></pre> <p>This will enable you to call <code>A.double(2) #= 4</code> and <code>A.quadruple(2) #=8</code>. Note that in a class method, <code>self</code> references that class because the class is the current object.</p> <h1>how the value of self is determined</h1> <p>The current value of self in a particular method is set to the object that that method was called upon. Normally this uses the '.' notation. When you run <code>some_object.some_method()</code>, self is bound to some_object for the duration of some_method, meaning that some_method can use self in one of the ways mentioned above.</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. 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.
    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