<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag variables</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/variables</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Whose Variable Is It Anyway?</title>
      <description>&lt;p&gt;The more I think about Ruby in relation to other object programming languages I&amp;#8217;ve worked with, the more I realize that there&amp;#8217;s a continuum of static vs. dynamic typing.&lt;/p&gt;  
&lt;p&gt;Ruby fits close to one end of that continuum. Understanding this can help understand how to best use the language. I recently had a quick look at Russ Olsen&amp;#8217;s new book &lt;a href="http://www.amazon.com/gp/product/0321490452?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=0321490452"&gt;Design Patterns in Ruby&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=denhaven2com-20&amp;#38;l=as2&amp;#38;o=1&amp;#38;a=0321490452" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt; and looked at his section on the observer pattern.  I&amp;#8217;d just posted to ruby-talk about this pattern, how it was implemented in Smalltalk, and a more Rubyish implementation.  I&amp;#8217;ll get to that at the end of this article, but first I really feel the urge to talk about instance variables.&lt;/p&gt;
&lt;p&gt;If we view a type as a particular interpretation of a memory layout, I see something like this&lt;/p&gt;

	&lt;table style="border:1px solid black;"&gt;
		&lt;tr&gt;
			&lt;th&gt;Language &lt;/th&gt;
			&lt;th&gt;Outside &lt;/th&gt;
			&lt;th&gt;inside &lt;/th&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt; Java &lt;/td&gt;
			&lt;td&gt; static &lt;/td&gt;
			&lt;td&gt; static &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt; Smalltalk &lt;/td&gt;
			&lt;td&gt; encapsulated &lt;/td&gt;
			&lt;td&gt; static &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt; Ruby &lt;/td&gt;
			&lt;td&gt; encapsulated &lt;/td&gt;
			&lt;td&gt; dynamic &lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




&lt;p&gt;The two columns represent how the object&amp;#8217;s innards &amp;#8220;look&amp;#8221; from outside the object, and inside the object (i.e. inside a method) respectively.&lt;/p&gt;

&lt;p&gt;In Java, subject to access modifiers, instance variables, a.k.a. fields, can be directly accessed.  No accessor method is required.  In Smalltalk and Ruby, instance variables of an object are only accessible while executing one of the object&amp;#8217;s methods.  Although both languages provide a bypass mechanism, instance_variable_get and instance_variable_set for Ruby; instVarAt: and instVarAt:put: for Smalltalk, these are both methods, and are to be used only in &amp;#8220;emergencies&amp;#8221; since they break the encapsulation of the object.&lt;/p&gt;

&lt;h2&gt;Static Instance Variable Binding&lt;/h2&gt;
&lt;p&gt;By static here, I mean that the code which accesses the instance variable uses information which is statically bound by the compiler. This is a subtlety which misses a lot of today&amp;#8217;s programmers &lt;a href="http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html"&gt;who don&amp;#8217;t understand what a compiler does,&lt;/a&gt; which is to take the textual, human written and readable source code and turn it into bits and bytes which can be executed by some form of computer.  The older wiser guys might just want to skim this section.&lt;/p&gt;
&lt;p&gt;That computer might be a real computer, like an Intel processor, or a virtual computer in the form of a software implemented virtual machine or interpreter. In the case of a real or virtual machine, there is an instruction set which gives the repertoire of the machine.  The program is executed by moving step by step, instruction by instruction. Now, if we have a simple C statement like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_C "&gt;int a = b&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then the instruction sequence for an imaginary computer might be something like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    load  reg2, 20(reg1)
    store reg2, 40(reg1)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which loads the second machine register from a word which is at an address 20 bytes after the address contained in machine register 1, and then stores that value into another word offset 40 bytes from register 1.  Here a and b are intended to be local temporary variables, and I&amp;#8217;ve decided that my compiler is using register 1 to point to the current activation stack frame.  Those magic numbers, 20 and 40 are computed by the compiler as part of it&amp;#8217;s function to map variables to memory locations.&lt;/p&gt;
&lt;p&gt;The idea that instructions might be different lengths is quite common in instruction set design.  Usually some number of bits at the beginning of the instruction is used to encode an &amp;#8216;op code&amp;#8217; like load or store, add, or subtract, etc.  Other bits are used to determine the presence and format of parameters for the operation.  Different instruction sets have different addressing modes, which allow memory to be addressed in various ways, such as the mode used above which addresses memory as an offset from a location held in a register. Other addressing modes might add another register used to index elements in an array, for example. Most real instruction sets have some unit of length for instructions, so for a given machine architecture, all instructions might be one or more words, or one or more bytes.&lt;/p&gt;
&lt;h2&gt;Bytecodes Are An Instruction Set Format&lt;/h2&gt;
&lt;p&gt;The term &amp;#8220;bytecode&amp;#8221; is simply a particular form of an instruction set, or rather a family of forms.  Most people associate the term with Java, and a particular instruction set, although the term predates Java, being used by Smalltalk and probably before.  It really means that the &amp;#8216;machine code&amp;#8217; instructions are represented as a series of bytes.  Many instructions are encoded by a single byte, although some require additional bytes in order to form a complete instruction. The general term bytecode simply means that the length unit for the instruction set is one byte.&lt;/p&gt;
&lt;p&gt;Although Java and Smalltalk implementations typically use bytecode instruction sets for their virtual machines, the actual set of bytecodes differs, much as the instruction set for an Intel Core Duo 2, differs from the instruction set for a PowerPC G4.&lt;/p&gt;
&lt;h2&gt;Classical Instance Variable Binding, Smalltalk Style&lt;/h2&gt;
&lt;p&gt;Now lets look at similar code in Smalltalk.  In this article, I&amp;#8217;m using the bytecodes defined by the out of print, &amp;#8220;Smalltalk:The Language and Its Implementation&amp;#8221;, a.k.a. &amp;#8220;The Blue Book&amp;#8221;, other Smalltalk implementations might be slightly different.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s say that a and b here are instance variables.  The Smalltalk bytecodes&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    a := b&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Would look something like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    push_iv_4            # push instance-variable #4 
                             # onto the stack
    store_and_pop_iv_6   # store the top of the stack in
                             # instance-variable #6&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In Smalltalk, those magic index numbers used to access instance variables are determined when the class definition is saved. In this case b turned out to be the 4th instance variable, and a the 6th.  Smalltalk bytecodes are optimized for small objects, the first 16 instance variables can all be pushed or popped with a single-byte instruction, if an object has more than 16 instance variables then those beyond the 16th need to be accessed via an extended push or store instruction, which allows up to 64 instance variables to be accessed.&lt;/p&gt;
&lt;p&gt;In Smalltalk, although instance variables aren&amp;#8217;t &lt;strong&gt;typed&lt;/strong&gt;, they are &lt;strong&gt;declared&lt;/strong&gt; in a class definition message executed when the class definition is saved.  Any time a class definition is saved, the indices for the instance variables of that class, and of any subclasses are (re)computed, and any methods in the class and it&amp;#8217;s subclasses are re-compiled to adjust the offsets. The instance variables defined in the topmost class get the first n offsets, each immediate subclasses instance variables get sequential offsets starting with the next available, and so forth.&lt;/p&gt;
&lt;p&gt;This is why I said above that inside a Smalltalk object, i.e. within it&amp;#8217;s methods, the object is mapped statically.  Changing the instance variable definitions requires re-compilation to avoid &amp;#8216;type-errors&amp;#8217; in the methods.&lt;/p&gt;
&lt;p&gt;Note that those &amp;#8216;emergency-only&amp;#8217; methods instvarAt: and instVarAt:put: map to the push_iv and store_and_pop_iv bytecodes, the first argument to both is the instance variable index.  This also means that they need to be used with care, since you need to know the offset of the instance variable. Now, at least Smalltalk can tell you if you try to access a non-existant instance variable slot but it can&amp;#8217;t tell that you&amp;#8217;re accessing the &lt;strong&gt;wrong&lt;/strong&gt; slot.&lt;/p&gt;
&lt;h2&gt;Java Field Binding&lt;/h2&gt;
&lt;p&gt;In Java, offsets are not compiled directly into the bytecodes, there&amp;#8217;s a level of indirection. Peter Haggar, with whom  I used to work at &lt;span class="caps"&gt;IBM&lt;/span&gt; wrote an &lt;a href="http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/"&gt;article on Java bytecodes&lt;/a&gt; on developerworks. I hope he won&amp;#8217;t mind if I borrow one of his examples. Here&amp;#8217;s a simple accessor method&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public String employeeName()
{
    return name;
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
And the resultant bytecodes. (The 0, 1, and 4 are offsets from the beginning of the method)
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    Method java.lang.String employeeName()
    0 aload_0
    1 getfield #5 &amp;lt;Field java.lang.String name&amp;gt;
    4 areturn&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What this code does first is to push a reference to the current object, &lt;strong&gt;this&lt;/strong&gt;, onto the stack.  Then the getfield instruction uses it&amp;#8217;s operand to replace the top two items on the stack with the value of the field.  So these two byte-codes (actually 3 bytes in total) are roughly equivalent to the Smalltalk push_iv bytecode, but for two differences:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;The Smalltalk push_iv opcode implicitly uses the receiver of the current method, whereas the getfield opcode needs another argument referencing the object whose instance variable is being accessed.&lt;/li&gt;
    &lt;li&gt;In Smalltalk the argument identifying the variable is an integer index, but in Java the argument is actually a reference to a field descriptor associated with the class of the object whose instance variable is being referenced.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first difference is because in Java, unlike in Smalltalk, you can directly get and set public fields outside of of the objects methods, so since the object in question isn&amp;#8217;t implied, it has to be specified.&lt;/p&gt;
&lt;p&gt;The second difference is to allow for separate compilation.  The actual Java VM specification doesn&amp;#8217;t dictate how fields are mapped within objects, but the abstraction is to allow this mapping to be adjusted at the time classes are loaded.  If a subclass is compiled separately from it&amp;#8217;s superclass, it might get a new starting position for it&amp;#8217;s fields everytime it&amp;#8217;s loaded if one or it&amp;#8217;s superclasses has added or removed fields.&lt;/p&gt;
&lt;p&gt;So in order to access a Java field, the compiler needs to know the type of the object containing the field.  This is true whether we are inside a method or outside.&lt;/p&gt;
&lt;h2&gt;Instance Variables, The Ruby Way&lt;/h2&gt;
&lt;p&gt;In Ruby, instance variables aren&amp;#8217;t declared, so offsets can&amp;#8217;t be assigned statically. Instead, Ruby looks up instance variables dynamically, using the instance variable &lt;strong&gt;name&lt;/strong&gt; rather than an offset. Again this matches the &amp;#8216;emergency use&amp;#8217; messages, instance_variable_get and instance_variable_set take an instance variable name, complete with the &amp;#8221;@&amp;#8221; sigil, where the Smalltalk instVarAt: methods take an integer.&lt;/p&gt;
&lt;p&gt;In Ruby 1.8, this lookup is implemented in a fairly straightforward fashion. With a few exceptions, which I won&amp;#8217;t take the time to go into here, a Ruby object has a pointer named iv_tbl which points to a hash table which maps the instance variable names to values.  In Ruby 1.9, the implementation is a bit more clever, but the effects are the same.&lt;/p&gt;
&lt;h2&gt;So Whose Variable IS it Anyway?&lt;/h2&gt;
&lt;p&gt;Which brings us back to the title of this article.  In Java and Smalltalk, every instance of a given class has the same set of instance variables, albeit each with it&amp;#8217;s own value. The variables come into existence when they are declared, and the class is compiled or the class definition is saved.&lt;/p&gt;
&lt;p&gt;One thing I didn&amp;#8217;t mention in the discussion of Smalltalk is that, because traditional Smalltalk implementations don&amp;#8217;t separate the development environment from the run-time environment, when a class definition changes, besides requiring method recompilation for the class and it&amp;#8217;s subclasses, any existing instance variables need to be mutated to either add or remove the changed instance variables.  Back when he was working on the language self, which has dynamic resolution of instance variables like Ruby, Dave Ungar used to like to kill various Smalltalk implementations by adding an instance variable to the Object class.  The problem is that because we are trying to operate on the running system, the system usually trips over itself during such a change.  I tried this a few weeks ago with Squeak, and although it warned me twice that I shouldn&amp;#8217;t do that, it did try when I clicked that second &amp;#8220;Are you sure&amp;#8221; button, and crashed pretty quickly. Ruby does handle this as a matter of course, since instance variables are only added to individual objects when they are needed, and self inside a method really is duck-typed, actually more than duck-typed, since the needed instance variables appear just in time.&lt;/p&gt;
&lt;h2&gt;So you mentioned the Observer Pattern, What&amp;#8217;s All This Have To Do With That&lt;/h2&gt;
&lt;p&gt;One of the things which got me thinking about this again was a thread on ruby-talk some weeks ago about Ruby garbage collection and some of the things which keep Object from being considered garbage and being collected.  The Ruby GC tends to have problems if you use finalization and aren&amp;#8217;t &lt;strong&gt;really&lt;/strong&gt; careful about how you define your finalizers.&lt;/p&gt;
&lt;p&gt;One of the classic gotcha&amp;#8217;s in Smalltalk in this vein is the implementation of Object dependents, a.k.a. the Observer Pattern.  Smalltalk provides a mechanism to add dependent objects to any other object which, when it want&amp;#8217;s to notify it&amp;#8217;s dependents that it has changed, can simply send itself the changed message, which in turn sends each dependent the message update: with the changed object as the argument.&lt;/p&gt;
&lt;p&gt;This is the basis of the Model View Controller design in Smalltalk. Views register as dependents on Models, and when a model changes, any Views depending on it can react. This is the genesis of the Observer pattern from the &lt;a href="http://www.amazon.com/gp/product/0201633612?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=0201633612"&gt;well known gang of four Design Patterns book&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=denhaven2com-20&amp;#38;l=as2&amp;#38;o=1&amp;#38;a=0201633612" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt; where Model, and View have been generalized to Subject and Observer respectively.&lt;/p&gt;
&lt;p&gt;In Smalltalk the ability to manage a list of dependents and notify them on changes is something that every object can do, but very few objects actually use this capability.  In order to avoid having an instance variable in every Smalltalk object to reference a dependents collection which is almost always empty, the default implementation actually keeps a global hash which maps objects with dependents to their dependent collection.&lt;/p&gt;
&lt;p&gt;The problem with this default implementation is that once an object gains a dependent, the object and it&amp;#8217;s dependent objects are permanently reachable, and therefore ineligible for garbage collection, unless the dependency is explicitly removed.  As a result of this, the classes of most objects which actually &lt;strong&gt;have&lt;/strong&gt; dependents reimplement the default methods to refer to the dependents collection via an instance value in the object with dependents. Squeak, for example provides a subclass of object called Model which provides such a GC friendly implementation.&lt;/p&gt;
&lt;p&gt;Which brings me to the implementation of the observer pattern in Ruby.  In his discussion of this pattern in his book, Russ Olsen provides a module which can be mixed into an object to allow it to have dependents:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Subject&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;
    &lt;span class="attribute"&gt;@observers&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;[]&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;add_observer&lt;/span&gt;&lt;span class="punct"&gt;(&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="attribute"&gt;@observers&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;observer&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;delete_observer&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="attribute"&gt;@observers&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;delete&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;notify_observers&lt;/span&gt;
    &lt;span class="attribute"&gt;@observers&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
      &lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;  
&lt;span class="keyword"&gt;end&lt;/span&gt;    &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a nice Ruby spin on the pattern, in that the Observers can be blocks, or any other object which responds to a call method which takes the Subject as its argument.&lt;/p&gt;
&lt;p&gt;Shortly before seeing the book, as a result of that GC thread, I&amp;#8217;d written my own variation on this, which let&amp;#8217;s any object be a subject, by opening up the Object class:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Object&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;add_observer&lt;/span&gt;&lt;span class="punct"&gt;(&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="attribute"&gt;@observers&lt;/span&gt; &lt;span class="punct"&gt;||=&lt;/span&gt; &lt;span class="punct"&gt;[])&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;observer&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;delete_observer&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="ident"&gt;observers&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;delete&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;notify_observers&lt;/span&gt;
      &lt;span class="ident"&gt;observers&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
        &lt;span class="ident"&gt;observer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

    &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;observers&lt;/span&gt;
      &lt;span class="attribute"&gt;@observers&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="punct"&gt;[]&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt; 
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because of the fact that Ruby only adds instance variables on the fly as needed, we get the benefit of universal support for objects to be Subjects without requiring an observers instance variable for those objects which don&amp;#8217;t. The only cost is the potential namespace collision for the four method names.&lt;/p&gt;
&lt;h2&gt;Another Use of Dynamic Instance Variables&lt;/h2&gt;
&lt;p&gt;Recently I wrote an &lt;a href="http://www.infoq.com/news/2008/01/rails-resource-controller"&gt;article for InfoQ&lt;/a&gt; about &lt;a href="http://jamesgolick.com/resource_controller"&gt;James Golick&amp;#8217;s resource_controller plugin for Rails&lt;/a&gt; which allows you to write Rails controllers for RESTful resources which can automatically adapt for use in different resource nesting contexts.  This plugin makes good use of the dynamic nature of Ruby instance variables, automatically defining different instance variables in the controller to correspond to the end resource and each of its parent resources.&lt;/p&gt;
&lt;h2&gt;Whew!&lt;/h2&gt;
&lt;p&gt;This has turned out to be a rather long article, which I&amp;#8217;ve been meaning to write for some time.  I hope that someone finds it useful, or at least interesting.&lt;/p&gt;</description>
      <pubDate>Fri, 08 Feb 2008 16:14:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:bfb1599b-784b-403b-8fdf-90e68ecaa897</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>java</category>
      <category>binding</category>
      <category>variables</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/487</trackback:ping>
    </item>
    <item>
      <title>On Variables, Values, and Objects</title>
      <description>&lt;p&gt;I&amp;#8217;ve recently observed some posts to ruby-talk which evidence some confusion on the part of the posters about the relationship between variables and objects in Ruby.  One currently active thread concerns several participants who are upset that instances of the Matrix class in the Ruby standard library can&amp;#8217;t be changed once created.&lt;/p&gt;


	&lt;p&gt;In Ruby, like in other uniformly object-oriented languages, the relationship between variables and their values is subtly different than in other languages, and this is a crucial paradigm shift, which must be crossed in order to understand Ruby.&lt;/p&gt;


	&lt;p&gt;In many languages a variable names an area in memory which holds a &amp;#8220;bag-of-bits&amp;#8221; representing the value of the variable.  The size of that area depends on the type of the variable.  A variable holding an integer might be 4-bytes long, while one holding a particular structure might be 325 (or whatever) bytes.&lt;/p&gt;


	&lt;p&gt;In a uniformly object-oriented language, &lt;strong&gt;all&lt;/strong&gt; variables reference objects, and &lt;strong&gt;any&lt;/strong&gt; variable can reference &lt;strong&gt;any&lt;/strong&gt; object, or different objects over time.  My good friend at &lt;span class="caps"&gt;IBM&lt;/span&gt;, the late David N. Smith, used to say that in such a language, &amp;#8220;all variables are the same size,&amp;#8221; when he wrote about or taught Smalltalk.&lt;/p&gt;


	&lt;p&gt;This distinction can trip up the unwary.  Let&amp;#8217;s try to clear some of the stumbling blocks out of the way.&lt;/p&gt;


&lt;h2&gt;Ruby Variables Don&amp;#8217;t Have (Permanent) Classes&lt;/h2&gt;
In one recent thread, someone suggested adding a method to the &amp;#8220;class of a variable.&amp;#8221; 

The problem with this idea, is that a variable doesn&amp;#8217;t really have &lt;strong&gt;a&lt;/strong&gt; class.  For example, consider thiscode:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;require 'matrix'
   a = 5
   a = a * 1.0
   a = a * Matrix.identity(3)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Here the variable a refers to a sequence of objects, each with a different class.  First a Fixnum, then a Float, then a Matrix.  While Fixnum, and Float inherit from Numeric, Matrix doesn&amp;#8217;t but it can form a duck-type with Numeric in many uses.&lt;/p&gt;


This also illustrates that &amp;#8220;all variables are the same size.&amp;#8221;  In a language like C, a variable holding an integer, will probably have a diffferent size as one holding a double (which is what Ruby defines as the representation of a float), and one which holds a 3&amp;#215;3 matrix of doubles certainly will be bigger than one holding a scalar double.
&lt;h2&gt;Ruby Variables Hold Object References, not Object State&lt;/h2&gt;
The magic which allows this is the fact that in Ruby, as in  Smalltalk, a variable doesn&amp;#8217;t hold the &amp;#8220;bag-of-bits&amp;#8221; which represents a particular data-structure. Instead it contains a reference to an object which holds the &amp;#8220;bag-of-bits.&amp;#8221;  The actual structure of that &amp;#8220;bag-of-bits&amp;#8221; is of no concern to, and is hidden from the variable and it&amp;#8217;s users.

	&lt;p&gt;This encapsulation barrier is key to what makes a language truly &amp;#8220;object-oriented,&amp;#8221; at least the way I use the term.&lt;/p&gt;


	&lt;p&gt;The &amp;#8220;bag-of-bits&amp;#8221; which represents a particular object &lt;strong&gt;is&lt;/strong&gt; visible to the methods of that object&amp;#8217;s class, at least in an abstract way. Some details are even hidden to those methods at the Ruby source code level, and are tied up in the language implementation, hand written extensions to Ruby usually do need to deal with those details.&lt;/p&gt;


	&lt;p&gt;In Smalltalk, the pseudo-variable &lt;i&gt;self&lt;/i&gt; is actually strongly typed in the C-sense since instances of Smalltalk classes have a fixed structure, and &lt;i&gt;self&lt;/i&gt; in a method is guaranteed to refer to an instance of the class owning the method or one of it&amp;#8217;s subclasses, and instance variables are found in known slots within the object.&lt;/p&gt;


	&lt;p&gt;In Ruby it&amp;#8217;s a little different because Ruby instance variables are created dynamically and accessed via a hash table within the object, at least in the 1.8.x implementation.&lt;/p&gt;


	&lt;p&gt;As a first approximation, we can initially think of variables as holding pointers to objects, but I&amp;#8217;ll expose this for the pedagogical lie that it is, a little later in this article.&lt;/p&gt;


&lt;h2&gt;Mutability, and Aliasing&lt;/h2&gt;
Here&amp;#8217;s one of those stumbling blocks for those who expect variables in a uniformly object-oriented language to work like they do in a language like C or Fortran:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt; 1: a = [1, 2, 3]
 2: b = [1, 2, 3]
 3: c = a

 4: a[1] = 0

 5: p a #=&amp;gt; [1, 0, 3]
 6: p b #=&amp;gt; [1, 2, 3]
 7: p c #=&amp;gt; [1, 0, 3]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Everything looks pretty normal up ot line 7. We never did anything with &lt;i&gt;c&lt;/i&gt; after we assigned it a value, but it  doesn&amp;#8217;t seem to have held it&amp;#8217;s value.&lt;/p&gt;


	&lt;p&gt;Or has it?&lt;/p&gt;


	&lt;p&gt;Well, it actually has, since it&amp;#8217;s value is a reference to an object, and that &lt;strong&gt;object&lt;/strong&gt; is the same one referenced by a.&lt;/p&gt;


Line 4 might &lt;i&gt;look&lt;/i&gt; like an assignment to the variable &lt;i&gt;a&lt;/i&gt;, but it&amp;#8217;s really a method call to the array which a happens to be referencing at the time.  And that method (called []=) changes, or &lt;i&gt;mutates&lt;/i&gt; that array.  That change will be visible through the variables &lt;i&gt;a&lt;/i&gt;, &lt;i&gt;c&lt;/i&gt; and any others that reference that particular array.  Multiple references to the same object are called aliases to that object.  They might be named variables, or referenced which are inside another object:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt; 8: d = [[1, 2, 3], [4, 5, 6]]
 9: e = d
10: d[1][1] = &amp;quot;Fred&amp;quot;
11: p e #=&amp;gt; [[1, 2, 3], [4, &amp;quot;Fred&amp;quot;, 5]]]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;While such mutating methods are often very useful, this effect of aliasing is something which the Ruby programmer who uses them needs to keep in mind.&lt;/p&gt;


	&lt;p&gt;Many built-in Ruby classes have both mutating, and non-mutating versions of methods, for example Array#compact returns a new array which is a copy of the receiver with nil elements removed, leaving the original array intact, while Array#compact! mutates the receiver and removes the nil&amp;#8217;s in-place.&lt;/p&gt;


	&lt;p&gt;As with &lt;i&gt;compact&lt;/i&gt; and &lt;i&gt;ocmpact!&lt;/i&gt;, it&amp;#8217;s standard practice to give such mutating methods a name with a trailing &amp;#8216;&lt;strong&gt;!&lt;/strong&gt;&amp;#8217; which serves as a warning that they mutate the receiver.  It&amp;#8217;s a warning to the wise.&lt;/p&gt;


&lt;h2&gt;That Little White/Pedagogical Lie, or Variables and Object Identity&lt;/h2&gt;
I said above that thinking that variables hold pointers to objects was a first approximation to reality.  The fact is they really don&amp;#8217;t

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;12: a1 = [1, 2, 3]
13: a2 = [1, 2, 3]
14: p a1.object_id #=&amp;gt; -605450882
15: p a2.object_id #=&amp;gt; -605225658&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The object_id method returns a number which is unique in the sense that no two active objects will ever have the same object_id, and a given object will always have the same object_id.  In the Ruby 1.8.x implementation, object_id actually just gives the bits of the value used to reference the object in the guise of a Fixnum.&lt;/p&gt;


So we see here that a1 and a2 are diffferent objects.  Let&amp;#8217;s try a few more objects:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;14: i1 = 1
15: i2 = 1
16: p i1.object_id #=&amp;gt; 3
17: p i2.object_id #=&amp;gt; 3
18: sym1 = :sym
19: sym2 = :sym
20: p sym1.object_id #=&amp;gt; 4090126
21: p sym2.object_id #=&amp;gt; 4090126 &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Here&amp;#8217;s another surprise.  Before when we created two different arrays, we ended up with two different objects. But even though we never assigned i2 to i1, or sym2 to sym1, the &amp;#8216;two&amp;#8217; 1s have the same object_id, and the &amp;#8216;two&amp;#8217; symbols named :sym share an object_id, what&amp;#8217;s that about?&lt;/p&gt;


	&lt;p&gt;Certain classes ensure that if &amp;#8216;two&amp;#8217; of their instances are equal they are the same object.  We can see that Fixnum, and Symbol do this, others are NilClass, TrueClass, and FalseClass.  For symbols, this conflation of value and identity is part of their definition, for the others, it&amp;#8217;s probably not strictly necessary, but it makes some common operations like testing for equality, and in the case of Fixnums arithmetic operations, much faster.&lt;/p&gt;


What&amp;#8217;s really held in variables is a value which can either be used to recognize that the object is one of these special objects, or that can be mapped efficiently to the real address of the object. The details are implementation-specific, and not really germane to the current topic, so I won&amp;#8217;t go into them in this article.
&lt;h2&gt;Immutable Objects&lt;/h2&gt;
It&amp;#8217;s often advisable to design classes without mutating methods.  For example, Fixnum has a method [] which returns the &lt;i&gt;n&lt;/i&gt;th bit of the representation of the number where bit 0 is the least significant bit. Suppose that it also had a method []=, which &lt;strong&gt;set&lt;/strong&gt; the value of the cooresponding bit:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;#Hypothetical code - with a tribute to Jimi Hendrix
h1: a = 6
h2: b = 6
h3: a[0] = 1; a[1] = 0; a[2] = 0; a[3] = 1
h3: p a #=&amp;gt; 9
h4: p b #=&amp;gt; 9&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Since all 6&amp;#8217;s are the same object, then if Fixnum had mutating methods, we could make all 6&amp;#8217;s turn out to be 9.  While Jimi might not mind, my program might not like it.&lt;/p&gt;


	&lt;p&gt;And it would cause head-scratching debugging sessions.  Thirty-two years ago, I found myself in just such a situation. Fortran II had a bug in the language specification which allowed the value of  integer literals to be changed when you passed a literal as an argument to a subroutine which assigned to that argument.  I spent over a day trying to figure out why my program didn&amp;#8217;t work.&lt;/p&gt;


	&lt;p&gt;For this reason, to quote Martha Stewart, it&amp;#8217;s a good thing that the Ruby numeric classes don&amp;#8217;t have mutating methods.  Even though Bignums and Floats don&amp;#8217;t conflate value with identity, it would probably be even more mystifying to find that some instance of 1.5 changed to 1.45 when other&amp;#8217;s didn&amp;#8217;t. To me it seems far better to head off that particular &amp;#8216;feature&amp;#8217; at the pass.&lt;/p&gt;


	&lt;p&gt;Another reason for this choice of immutability is taken from functional programming.  Although Ruby is not really an FP language, it takes many concepts from that paradigm. In FP, functions with side-effects are disallowed, and mutation is certainly a side-effect.  Taking FP to the limit makes doing things which require side-effects, like I/O, take special tricks.  Ruby allows side-effects, and doesn&amp;#8217;t &lt;strong&gt;require&lt;/strong&gt; an FP approach, but like other features like regular expressions, understanding functional approaches is a useful tool in the Ruby programmer&amp;#8217;s bag.&lt;/p&gt;


&lt;h2&gt;Immutability at the Instance Level&lt;/h2&gt;
Ruby Object&amp;#8217;s have a freeze method which makes a particular instance immutable:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;F1: a1 = [1, 2, 3]
F2: a2 = a1
F3: a1.freeze
F4: a1[1] = &amp;quot;Fred&amp;quot; #=&amp;gt; TypeError: can't modify frozen array
F5: a2[1] = &amp;quot;Fred&amp;quot; #=&amp;gt; TypeError: can't modify frozen array&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Beware, though, that this only freezes the object itself and not it&amp;#8217;s sub-objects, or referenced objects:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;F6: a3 = [[1, 2, 3], [4, 5, 6]]
F7: a3.freeze
F8: a3[0][1] = &amp;quot;Fred&amp;quot;
F9: p a3 #=&amp;gt; [[1, &amp;quot;Fred&amp;quot;, 3], [4, 5, 6]]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Matrix and Mutability&lt;/h2&gt;
Following the pattern established by Numerics, the designer of the Matrix class, who, according to the comments in the source code, ported it from Smalltalk, made Matrix instances immutable.

	&lt;p&gt;This upsets some who prefer to think of a Matrix as a 2-dimensional array rather than a mathematical construct which acts much like a numeric in linear algebra.&lt;/p&gt;


	&lt;p&gt;If you want to view matrices that way you&amp;#8217;ve got a few choices:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Swim-against the current: Monkey-patch Matrix to add mutating methods.&lt;/li&gt;
&lt;li&gt;Swim with the current: Use the Matrix#to_a and Matrix[] to get a mutable array and then create a new Matrix from it:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;M1: m1 = Matrix.identity(3)
M2: p.m1 #=&amp;gt; Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
M3: a1 = m1.to_a
M4  a1[1][1] = 3
M4  m1 = Matrix[a1]
M5: p m1 #=&amp;gt; Matrix[[[1, 0, 0], [0, 3, 0], [0, 0, 1]]]&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Find a new stream: look at other sources of a mutable 2-dimensional array, for example &lt;a href="http://narray.rubyforge.org/"NArray&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
The choice, gentle reader, is yours.  I hope that my efforts help you make it a more informed choice.</description>
      <pubDate>Wed, 13 Sep 2006 15:47:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:c2c1aa30-0c31-4708-8f00-3e7bd05be738</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>nubies</category>
      <category>variables</category>
      <category>objects</category>
      <category>objectidentity</category>
      <category>semantics</category>
      <category>implementation</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/29</trackback:ping>
    </item>
  </channel>
</rss>
