<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - The Rules of Ruby Self</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4883/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self" rel="alternate" type="text/html"/>

  <updated>2008-11-05T18:46:37-05:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:a30b2431-9397-414b-828b-39d683c76424</id>

    <published>2008-04-17T21:53:22-04:00</published>
    <updated>2008-04-17T21:53:22-04:00</updated>
    <title type="html">The Rules of Ruby Self</title>
    <link href="http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;At the &lt;a href=&quot;http://novarug.org/articles/2008/04/10/april-meeting&quot;&gt;NovaRUG meeting yesterday&lt;/a&gt;, Dave Thomas gave a great talk on how the object model works in Ruby.  One of his main points was that in Ruby you are always sending a message to an object.  If you don&apos;t explicitly say which object you want to send the message to, Ruby sends it to self.  In order to make Ruby as clear a language as it is, self changes during the execution of your code.  To mentally keep track of what self is, you only need to understand rules of how self changes.  There are only 2 things that change what self points to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Explicit receiver of method call&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts self #self is &quot;main&quot;, the top-level object instance
Object.new  #self is temporarily changed to the class Object, but then back to main
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;class/module definition&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts self #Again, self is &quot;main&quot;
class Foo
  puts self #self is now Foo
end
#self is back to being main
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you think of it this way, it should be easy to always figure out what self is pointing to.  Being able to hold the mental picture of the changes to self as you read through code can make it easily to follow what is going on.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;At the &lt;a href=&quot;http://novarug.org/articles/2008/04/10/april-meeting&quot;&gt;NovaRUG meeting yesterday&lt;/a&gt;, Dave Thomas gave a great talk on how the object model works in Ruby.  One of his main points was that in Ruby you are always sending a message to an object.  If you don&apos;t explicitly say which object you want to send the message to, Ruby sends it to self.  In order to make Ruby as clear a language as it is, self changes during the execution of your code.  To mentally keep track of what self is, you only need to understand rules of how self changes.  There are only 2 things that change what self points to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Explicit receiver of method call&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts self #self is &quot;main&quot;, the top-level object instance
Object.new  #self is temporarily changed to the class Object, but then back to main
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;class/module definition&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts self #Again, self is &quot;main&quot;
class Foo
  puts self #self is now Foo
end
#self is back to being main
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you think of it this way, it should be easy to always figure out what self is pointing to.  Being able to hold the mental picture of the changes to self as you read through code can make it easily to follow what is going on.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>Vidar Hokstad</name>
    </author>
    <id>urn:uuid:eb62abb6-deef-47c3-a006-d7c1d2c7b8e9</id>
    <published>2008-04-18T08:44:19-04:00</published>
    <updated>2008-04-18T08:44:19-04:00</updated>
    <title type="html">Comment on "The Rules of Ruby Self" by Vidar Hokstad</title>
    <link href="http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self#comment-5109" rel="alternate" type="text/html"/>
    <content type="html">It&apos;s not strictly true it only happens in two cases. instance_eval and class_eval also change self:&lt;br/&gt;&lt;br/&gt;class Test&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;p self&lt;br/&gt;Test.new.instance_eval do&lt;br/&gt;  p self&lt;br/&gt;end&lt;br/&gt;&lt;br/&gt;Though these ones are fairly obvious, beware of the situations where people do &amp;quot;some_object.instance_eval { yield }&amp;quot; behind your back, as &amp;quot;self&amp;quot; may not be what you think it should be within a block you pass to a method.&lt;br/&gt;&lt;br/&gt;</content>
  </entry>
  <entry>
    <author>
      <name>Paul Barry</name>
    </author>
    <id>urn:uuid:cb66e8ee-81c3-4f7f-b8e6-84913ce36843</id>
    <published>2008-04-18T10:37:05-04:00</published>
    <updated>2008-04-18T10:37:05-04:00</updated>
    <title type="html">Comment on "The Rules of Ruby Self" by Paul Barry</title>
    <link href="http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self#comment-5110" rel="alternate" type="text/html"/>
    <content type="html">@Vidar&lt;br/&gt;&lt;br/&gt;In that example the explicit receiver of the instance_eval method call is the new Test instance returned by Test.new, so that still falls under Rule #1.</content>
  </entry>
  </feed>