<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dyrathror &#187; Ruby</title>
	<atom:link href="http://www.dyrathror.com/category/programming/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dyrathror.com</link>
	<description>Stay tuned ...</description>
	<lastBuildDate>Sat, 22 May 2010 09:26:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Objects, Class-Objects and MetaClass-Objects</title>
		<link>http://www.dyrathror.com/2009/07/ruby-objects-class-objects-and-metaclass-objects/</link>
		<comments>http://www.dyrathror.com/2009/07/ruby-objects-class-objects-and-metaclass-objects/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 04:56:35 +0000</pubDate>
		<dc:creator>Dyrathror</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.dyrathror.com/?p=368</guid>
		<description><![CDATA[After a while of evaluation I started over programming mainly in Ruby about two month ago.
Long time ago I learned for a short while object orientation programming in Smalltalk-80 but soon started to use C++, first with the AT&#38;T preprocessor and later with the gcc. Since 1998 I am programming Java. All these years have [...]]]></description>
			<content:encoded><![CDATA[<p>After a while of evaluation I started over programming mainly in Ruby about two month ago.</p>
<p>Long time ago I learned for a short while object orientation programming in Smalltalk-80 but soon started to use C++, first with the AT&amp;T preprocessor and later with the gcc. Since 1998 I am programming Java. All these years have left me with a certain idea of practical object orientation which was not really what I learned from Smalltalk-80.</p>
<p>But Ruby changed things a lot as I have to admit. In Ruby <strong>everything is an object</strong> and Ruby is interpreted like Smalltalk! What are the consequences. This is what my post will try to figure out.</p>
<blockquote><p>
<a href="http://www.ruby-doc.org/core/">From Ruby Core &#8211; Class documentation</a>:</p>
<p>Classes in Ruby are first-class objects—each is an instance of class Class.
</p></blockquote>
<div style="font-size:x-small;">
A big help for understanding Ruby Objects has been <a href="http://pragprog.com/titles/ruby3/programming-ruby-1-9">the Pickaxe Book</a>.
</div>
<p></p>
<p>In Ruby everything is an object. This includes as well Integers as Classes. So what happens if you write something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  a = <span style="color:#996600;">&quot;test1&quot;</span>
  b = <span style="color:#996600;">&quot;test2&quot;</span></pre></div></div>

<p>You get references <em>a</em> and <em>b</em> to your Objects <strong>a</strong> and <strong>b</strong> of class String which is an object too and has the class object Object as superclass. So far the only difference is that the Classes are realized as objects themselves.</p>

<a href="http://www.dyrathror.com/wp-content/gallery/programming/ruby-objects1.jpg" title="ruby objects" class="shutterset_singlepic188" >
	<img class="ngg-singlepic ngg-center" src="http://www.dyrathror.com/wp-content/gallery/cache/188__600x260_ruby-objects1.jpg" alt="ruby objects" title="ruby objects" />
</a>

<p>But you can do things in Ruby which you cannot do in C++ or Java. For example you can add a method only to one object like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  c = <span style="color:#996600;">'test'</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> c
    <span style="color:#9966CC; font-weight:bold;">def</span> double
      <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  c.<span style="color:#9900CC;">double</span>
  <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;testtest&quot;</span></pre></div></div>

<p>This piece of code generates a virtual class exclusively for the object <em>c</em>.</p>
<p>What happens if you try the same for integers? If you try the same for integers it should look like object <em>b</em> in this graph:</p>

<a href="http://www.dyrathror.com/wp-content/gallery/programming/ruby-objects2.jpg" title="ruby object class" class="shutterset_singlepic189" >
	<img class="ngg-singlepic ngg-center" src="http://www.dyrathror.com/wp-content/gallery/cache/189__600x255_ruby-objects2.jpg" alt="ruby object class" title="ruby object class" />
</a>

<p>The code for this would be:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  b = <span style="color:#006666;">7</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> b
    <span style="color:#9966CC; font-weight:bold;">def</span> double
      <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC00FF; font-weight:bold;">TypeError</span>: no virtual <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#CC00FF; font-weight:bold;">Fixnum</span></pre></div></div>

<p>So what is this? I thought that all objects can have virtual (or singleton) classes which hold the methods specially attached to this object?</p>
<p>The solution is quite simple. There are special classes like Fixnum which are called eigenclasses. Instead of a reference to an object these classes return the class object itself (for example as parameter for a method call). Eigenclass objects only have one single Object for example for each Fixnum value. This is why it is not possible to create singleton classes for eigenclass objects.</p>
<p>But what you can do is to extend an eigenclass with your own attributes like:
</pre>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Fixnum</span>
    attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  b = <span style="color:#006666;">5</span>
  b.<span style="color:#9966CC; font-weight:bold;">class</span>
  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Fixnum</span>
  b.<span style="color:#9900CC;">name</span> <span style="color:#996600;">'five'</span>
  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;five&quot;</span></pre></div></div>

<p>The next thing I want to do is to define a class variable. I found a nice article about class variables in Ruby on <a href="http://railstips.org/2006/11/18/class-and-instance-variables-in-ruby">John Nunemakers blog</a>. At first I was really astonished about this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> Polygon
    @@sides = <span style="color:#006666;">10</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">sides</span>
      @@sides
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> Polygon.<span style="color:#9900CC;">sides</span> <span style="color:#008000; font-style:italic;"># =&gt; 10</span></pre></div></div>

<p>This is a Polygon class with a class variable which holds the number of sides of the Polygon and a getter class method. Now we create a subclass which inherits from Polygon and holds its own number of sides:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> Triangle <span style="color:#006600; font-weight:bold;">&lt;</span> Polygon
    @@sides = <span style="color:#006666;">3</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> Triangle.<span style="color:#9900CC;">sides</span> <span style="color:#008000; font-style:italic;"># =&gt; 3</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> Polygon.<span style="color:#9900CC;">sides</span> <span style="color:#008000; font-style:italic;"># =&gt; 3</span></pre></div></div>

<p>What happens here? The Polygon sides have been set to 10 originally.</p>
<p>To understand this behavior you must have an understanding about the Ruby Meta-class and look-up mechanism for methods and variables. When you create a class variable or method Ruby actually creates the following Meta-Class for you:</p>

<a href="http://www.dyrathror.com/wp-content/gallery/programming/ruby-objects3.jpg" title="ruby meta-class" class="shutterset_singlepic196" >
	<img class="ngg-singlepic ngg-center" src="http://www.dyrathror.com/wp-content/gallery/cache/196__400x493_ruby-objects3.jpg" alt="ruby meta-class" title="ruby meta-class" />
</a>

<p>The Meta-Class hold the Class level extensions of your class. If you subclass Polygon now you have the following image:</p>

<a href="http://www.dyrathror.com/wp-content/gallery/programming/ruby-objects4.jpg" title="ruby meta-class subclassed" class="shutterset_singlepic197" >
	<img class="ngg-singlepic ngg-center" src="http://www.dyrathror.com/wp-content/gallery/cache/197__600x454_ruby-objects4.jpg" alt="ruby meta-class subclassed" title="ruby meta-class subclassed" />
</a>

<p>In Ruby everything, including class definitions, are executable code. The assignment to the class variable @@sides in the class Triangle leads the Ruby interpreter to search for an existing class variable @@sides which he finds in the Meta-Class of the superclass Polygon first and makes an assignment to this variable instead of creating a new meta-Class with the same variable for Triangle.</p>
<p>So what can we do if we want to have a variable for the sides of our class which is unique to the according class? As the class itself is an object you can assign a variable to the class object</p>

<a href="http://www.dyrathror.com/wp-content/gallery/programming/ruby-objects5.jpg" title="ruby class object variable" class="shutterset_singlepic198" >
	<img class="ngg-singlepic ngg-center" src="http://www.dyrathror.com/wp-content/gallery/cache/198__600x420_ruby-objects5.jpg" alt="ruby class object variable" title="ruby class object variable" />
</a>

<p>You can do this with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> Polygon
    <span style="color:#0066ff; font-weight:bold;">@sides</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      attr_accessor <span style="color:#ff3333; font-weight:bold;">:sides</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>for the Class Polygon and the same for class Triangle. Be aware that you have to assign the accessors to the class object and not to the object itself.</p>
<p>I hope that my explanations are correct and can help other newbies in Ruby to understand the Class handling a little bit.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dyrathror.com/2009/07/ruby-objects-class-objects-and-metaclass-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
