title: How does .self work in Ruby?
body: <p>I'm trying to re-write enumerable methods just for practice. I do not understand how <code>self</code> works in my methods. I know how to use it and what it returns, but what does <code>self</code> refer to?</p>
  
  <p>Here's a sample of the <code>each</code> method I re-wrote to extend the <code>Enumerable</code> method [sic].</p>
  
  <pre><code>module Enumerable
    def my_each
      return self unless block_given?
      i = 0
      while i &lt; self.length
        yield(self[i])
        i+=1
      end
    end
  end
  </code></pre>
  
  <p>If someone can clarify how <code>self</code> works, I'd appreciate it. More specifically, I would like to know what <code>self.length</code> refers to and what role it plays in my code.</p>