<?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 java</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/java</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Big Dave on Video</title>
      <description>InfoQ recently published a &lt;a href="http://www.infoq.com/interviews/dave-thomas-programming-languages-soa-and-the-web"&gt;video interview with Dave Thomas (of &lt;span class="caps"&gt;OTI&lt;/span&gt; fame)&lt;/a&gt;.
&lt;p&gt;In his inimitable style, Dave covers lots of interesting topics in software development, both today and with a historical perspective.&lt;/p&gt;
&lt;p&gt;I agree with almost everything he says, and find the rest food for thought.&lt;/p&gt;
&lt;p&gt;His comments about Java as a platform are quite germane to the article I published yesterday.  If you&amp;#8217;ve been exposed to Big Dave before, you&amp;#8217;ll enjoy this, and if you haven&amp;#8217;t it&amp;#8217;s a good introduction.&lt;/p&gt;</description>
      <pubDate>Tue, 29 Apr 2008 22:24:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:57c38e47-d746-4687-9434-480b47fdcab7</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2008/04/29/big-dave-on-video</link>
      <category>BigDave</category>
      <category>java</category>
      <category>smalltalk</category>
      <category>ruby</category>
      <category>iconoclasts</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/497</trackback:ping>
    </item>
    <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>Where I Come From</title>
      <description>&lt;p&gt;Maybe I&amp;#8217;ve gotten a bit sensitive of late to the perception some Rubyists
seem to have of Smalltalk-bred Rubyists like me. So I thought that I might say a bit more of what
I think of the current and future of Ruby as a language.
What follows is much longer than what I expected it to be when I started writing it yesterday,
I hope that some will find it some combination of interesting, useful, thought-provoking, or
at least amusing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This weeks meeting of the &lt;a href="http://ruby.meetup.com/3/"&gt;raleigh.rb&lt;/a&gt;
group was a recap of railsconf 2007. Some of the attendees seemed to have gotten 
a feeling that Avi Bryant was attacking Ruby in his keynote.  I can&amp;#8217;t speak for
him, and I wasn&amp;#8217;t there so this might just be my reaction.&lt;/li&gt;
&lt;li&gt;Heard this week on ruby-talk in a discussion of the fact that Module#ancestors doesn&amp;#8217;t
include a singleton class of the receiver should it have one:
&lt;blockquote&gt;
I do not want to argue with the wise guys if it is an error &amp;#8211; I
clearly thought so but that is not important ;)
&lt;br/&gt;&lt;br/&gt;But it really would have saved me an hour of debugging if the doc
stated clearly that singletons are not included. I thought this might
help others and as it took me 20s to vim the missing line into class.c
&lt;br/&gt;&lt;br/&gt;
- Robert Dober
&lt;/blockquote&gt;
To which I replied:
&lt;blockquote&gt;
As far as I can tell, singleton classes aren&amp;#8217;t mentioned in the doc.
The documentation borders on folklore.
&lt;br/&gt;&lt;br/&gt;
Singletons as a means of implementing both individual instance, and
class behavior have a position like &amp;#8220;the man behind the curtain&amp;#8221; in
&amp;#8220;The Wizard of OZ.&amp;#8221;  We&amp;#8217;re really supposed to disregard them. &lt;G&gt;
&lt;br/&gt;&lt;br/&gt;
Coming from a background in Smalltalk, my preference would be if this
machinery were more visible and official, but Matz has his reasons for
not doing so.  For one thing, not documenting it, and hiding it from
methods like ancestors and class makes it easier to tinker with as the
language evolves without &amp;#8220;officially&amp;#8221; breaking backward compatibility.
&lt;/blockquote&gt;
&lt;p&gt;Which prompted:
&lt;blockquote&gt;
Does that mean that no one who&amp;#8217;s ever used Smalltalk can ever think
that it&amp;#8217;s right for Ruby to deviate from Smalltalk? :-)  I ask in a
humorous spirit&amp;#8212;and also because it gives me an excuse to mention:
&lt;br/&gt;&lt;br/&gt;
&lt;a href="http://www.infoq.com/articles/coming-from-ruby"&gt;http://www.infoq.com/articles/coming-from-ruby&lt;/a&gt;
&lt;br/&gt;&lt;br/&gt;
&amp;#8212;David A. Black
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To answer David&amp;#8217;s humorously posed question, &lt;strong&gt;No&lt;/strong&gt;, 
and I present myself as &amp;#8220;exhibit A,&amp;#8221; because I certainly think that it&amp;#8217;s right
for Ruby to deviate from Smalltalk.  My main motivations in writing about Ruby in the context
of my Smalltalk background are first to help myself, and I hope others, understand Ruby a little
better by providing some perspective from another, earlier, dynamic language, and second, to a much
lesser extent, to air ideas of how some of the features of Smalltalk might be incorporated or
adapted as useful additions to Ruby as it evolves.
&lt;/p&gt;
&lt;p&gt;In many ways, I like Ruby better than I like Smalltalk.  To name a few reasons, Ruby is more dynamic, has a more flexible deployment &amp;#8216;model,&amp;#8217;, and 
albeit it&amp;#8217;s not a technical reason, it&amp;#8217;s not carrying the burden of the old business model of 
trying to get thousands of bucks for each development seat.&lt;p&gt;  
&lt;p&gt;This 
is what really allowed
Java to pop Smalltalk&amp;#8217;s balloon.  Smalltalk was seen by &lt;span class="caps"&gt;IBM&lt;/span&gt; and it&amp;#8217;s competitors as &amp;#8220;enterprise&amp;#8221; 
technology, and they expected the same kind of prices which
other corporate level software development
tools commanded.  When Java came out and was free &amp;#8220;as in beer,&amp;#8221; it gained a large popularity with
language hobbyists/hackers who could easily get it to play with, and a lot of those guys worked
for the companies to which &lt;span class="caps"&gt;IBM&lt;/span&gt;, ParcPlace-Digitalk, and the others were catering.&lt;/p&gt;
&lt;p&gt;The other aspect of Java which helped at the time was it&amp;#8217;s relationship in 
syntax, and a lot of its semantics, to C++ at a time when there was a lot of corporate C++
activity and some frustration was starting to be felt with the complexity of C++.  Java
felt enough like a simplified and friendlier C++ being a language which was more dynamic
without &amp;#8220;going all the way&amp;#8221; and which
felt familiar to both C++ advocates and C++ approach to
strongly typed class hierarchies, and C++ programmers with symptoms of the
&lt;a href="http://en.wikipedia.org/wiki/Stockholm_syndrome"&gt;Stockholm syndrome.&lt;/a&gt;
These self-same aspects of Java caused many Smalltalkers to be uneasy with
the language.&lt;/p&gt;
&lt;p&gt;The real salvation of Smalltalk, as it exists today, was and is Squeak.  I remember when I
first encountered Squeak, it was at a Birds-of-a-feather session at &lt;span class="caps"&gt;OOPSLA&lt;/span&gt;, right when Java was
really starting to get most of the mind-share.  My impression was that the atmosphere in the room
must have been somewhat like that in the Roman catacombs when the early Christians were 
hiding out.&lt;/p&gt;
&lt;p&gt;I guess that there is still a bit of the old &amp;#8220;Enterprise&amp;#8221; Smalltalk market, 
Instantiations, which took
over the old &lt;span class="caps"&gt;IBM&lt;/span&gt;/VisualAge Smalltalk base when it fell out of IBMs strategies, 
&lt;a href="http://www.instantiations.com/VAST/purchase.html"&gt;
sells it for an entry price of 
$6,995.&lt;/a&gt; There must be enough of the old legacy corporate Smalltalk customers around who 
buy it.&lt;/p&gt;
&lt;p&gt;Personally, it&amp;#8217;s been quite a while since I was an active Smalltalker, I made an oddyssey
through Java-land with IBMs shift in priorities.  I plan to write about some of my 
feelings during that period in retrospection but that&amp;#8217;s for a later date.&lt;/p&gt;
&lt;p&gt;I haven&amp;#8217;t been
keeping up with Squeak enough to know how and how much it has evolved the Smalltalk technology
and community.&lt;/p&gt;
&lt;h2&gt;Old Smalltalker&amp;#8217;s perceptions of Ruby&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;I always thought that Smalltalk would beat Java, I just didn&amp;#8217;t know that it would be called
&amp;#8216;Ruby&amp;#8217; when it did.&lt;/p&gt;
&lt;p&gt;&amp;#8212;Kent Beck&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I ran across this quote for the first time a few weeks ago.  It &lt;strong&gt;sounded&lt;/strong&gt; like
something that Kent would say, and also something that I would like to quote myself, but just
to make sure, I asked him.  He told me that &amp;#8216;yes&amp;#8217; he had said it in a private communication to
someone else, and both he and the original recipient had only recently found out that it was
becoming an internet meme.  Googling &lt;a href=""I always thought smalltalk would beat java"&gt;
&amp;#8220;I always thought that Smalltalk would beat Java&amp;#8221;&lt;/a&gt; garners over 250 hits.&lt;/p&gt;
&lt;p&gt;Kent tells me that he&amp;#8217;s a Ruby fan. Ward Cunningham is also. 
&lt;p&gt;I asked Ward, just before RailsConf,
if he was going since it was in his home-town.
He said that he had found out about it after
registration was closed, but he was planning to hang out at the hotel anyway.  From what I heard
last night he was recognized and invited to attend.&lt;p/&gt;
&lt;p&gt;Several of my other old Smalltalk buddies, who had also gone through a period with Java,
seem to have the same feeling which I do, that
looking at and using Ruby feels like being a widower who meets a new woman, who just seems to 
be a partial reincarnation of the former spouse.&lt;/p&gt;
&lt;h2&gt;Where You Are Come From Depends On Where You Came From&lt;/h2&gt;
&lt;p&gt;Getting to David Black&amp;#8217;s article.  He certainly has a point about letting Ruby be Ruby.  As
I&amp;#8217;ve said, I&amp;#8217;m quite happy with Ruby.&lt;/p&gt;
&lt;p&gt;Getting back to the early perceptions of Java, and making an analogy with natural languages
let me suggest that if we think of Smalltalk as Italian, and C++ as German, Java is
&lt;a href="http://en.wikipedia.org/wiki/Alsatian_language"&gt;Alsatian&lt;/a&gt;, and Ruby is French.&lt;/p&gt;
&lt;p&gt;Were it not for Squeak, Smalltalk might be Latin instead of Italian.&lt;/p&gt;
&lt;p&gt;The most un-Smalltalk like thing about Ruby, to me at least, is the syntax, 
Smalltalk has almost
none, many Smalltalkers say that Ruby has too much. I thought so initially, but now
I&amp;#8217;d argue that the flexibility and
&amp;#8220;sugarary&amp;#8221; aspects of Rubys syntax are what makes it so successful as a host for internal DSLs
like Rake, Rails/ActiveRecord, RSpec, etc. etc.  which would be far less
natural in Smalltalk with its extremely limited syntax.&lt;/p&gt;
&lt;p&gt;One of the complaints I often
heard about Smalltalk was it&amp;#8217;s &amp;#8216;different&amp;#8217; syntax just unary, binary, and keyword message send
expressions, blocks, parentheses for expression grouping, the use of &amp;#8217;;&amp;#8217; to send a message
to the object which got the last one,
and single value assignment, and a way to return a value, everything else,
including class and method definition, and control flow was built from these atoms.&lt;/p&gt;
Ruby probably appeals to a broader audience
since it looks enough like something like Java not to be considered too wierd, at least not at
first.  For one thing in ruby 1 + 2 * 3 is 7, instead of 9 in Smalltalk due to its total lack
of operator precedence.&lt;/p&gt; 
&lt;p&gt;But, syntax differences aside, conceptually both Smalltalk and
Ruby are romance languages, as compared to the Teutonic C++ and it&amp;#8217;s kinder-gentler relative.&lt;/p&gt;
&lt;h2&gt;Language Evolution and Cross-Breeding&lt;/h2&gt;
&lt;p&gt;Programming languages evolve much like natural languages.  They pick up influences from 
other languages and incorporate them with modifications.  English is the equivalent
of a multi-paradigm language, it has been influenced by the languages spoken by both the conquered
and counquerors of English speaking lands.  It&amp;#8217;s been influenced by Vikings, Normans, and Indians (both the Asian and American meanings of the last).
English has a lot of synonym pairs one from French and
one from Anglo-Saxon with it&amp;#8217;s Germanic roots.  Pork and pig; beef and cow; fraternity and brotherhood.  This comes from the days after the Norman conquest when the language of the English court
was French.  Bill Bryson describes this in his book 
&lt;a href="http://www.amazon.com/gp/product/0380715430?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=0380715430"&gt;The Mother Tongue&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=0380715430" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;&amp;#8221;&amp;#8221;The Mother Tongue&amp;#8221;.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
Many of the words considered &amp;#8220;fancy&amp;#8221; by English speakers are of French origin.  Kent 
Beck told me that while he was working in Zurich on a contract with a Swiss bank, he found that
an effective way of communicating with the programmers there, was to use simple English grammar,
but &amp;#8220;fancy&amp;#8221; English vocabulary.  The latter helped because the Swiss programmers facility with 
French increased the chances that they would share such word.&lt;/p&gt;
&lt;h2&gt;Multi-lingualism&lt;/h2&gt;
&lt;p&gt;There&amp;#8217;s an old joke which goes:&lt;/p&gt;
&lt;blockquote&gt;What do you call a person who speaks two languages? 
&lt;br/&gt;Bilingual.&lt;br/&gt;&lt;br/&gt;
And what do you call someone who speaks only one language?
&lt;br/&gt;An American.
&lt;/blockquote&gt;
&lt;p&gt;Most good programmers, even the Americans, are multi-lingual when it comes to programming 
languages. As it does with natural languages, this gives a broader perspective.&lt;/p&gt;
&lt;p&gt;But multi-lingualism sometimes makes it hard to compartmentalize language concepts.  
Once on a trip to Zurich,
I was having dinner with Erich Gamma, some other Zurich OTIers, and a couple of customers in a
very nice restaurant on the Bahnhofstrasse.  My facility with German fills a small thimble, 
but as I perused the large menu, with no obvious English to be seen, I realised that I was reading
it rather easily, and thought to myself, &amp;#8220;Hmmm, I&amp;#8217;m starting to understand German.&amp;#8221;  Then I 
realized
that the menu was in fact bi-lingual, but the second language was French, a language with which I do have some facility.&lt;/p&gt;
&lt;p&gt;I suspect that the &amp;#8220;coming from x&amp;#8221; quotes in David Black&amp;#8217;s article are the result of this 
difficulty in compartmentalizing by newcomers to Ruby.&lt;/p&gt;
&lt;h2&gt;A Living Language&lt;/h2&gt;
Programming languages, like natural languages, survive best when they evolve, this is what
distinguishes live languages from dead ones. Languages die as the number of speakers diminish, 
despite revival attempts like &lt;a href="http://www.amazon.com/gp/product/014015339X?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=014015339X"&gt;Winnie Ille Pu&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=014015339X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;, 
&lt;a href="http://www.amazon.com/gp/product/0865164207?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=0865164207"&gt;Quomodo Invidiosulus Nomine Grinchus Christi Natalem Abrogaverit: How the Grinch Stole Christmas in Latin&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=0865164207" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;,
&lt;a href="http://www.amazon.com/gp/product/1582348251?ie=UTF8&amp;#38;tag=denhaven2com-20&amp;#38;linkCode=as2&amp;#38;camp=1789&amp;#38;creative=9325&amp;#38;creativeASIN=1582348251"&gt;Harrius Potter et Philosophi Lapis (Harry Potter and the Philosopher&amp;#8217;s Stone, Latin Edition)&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=1582348251" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;, or 
the &lt;a href="http://la.wikipedia.org/wiki/Pagina_prima"&gt;Vicipaedia&lt;/a&gt;.
&lt;p&gt;Ruby is evolving, the core-team isn&amp;#8217;t asleep, right now ruby1.9 is the cauldron to which new
potions are being added to brew what will emerge as the next major Ruby revision.&lt;/p&gt;
&lt;p&gt;This is another area where Ruby and Smalltalk have similarities. 
Smalltalk started as a paper design by Alan Kay to win
a hallway bet challenging his assertion that he 
&amp;#8220;could define the &amp;#8216;most powerful language in the world&amp;#8217; in &amp;#8216;a page of code&amp;#8217;.&amp;#8221; 
Dan Ingalls then actually implemented it in Basic on a &lt;span class="caps"&gt;NOVA&lt;/span&gt; minicomputer, this 
begat Smalltalk-72,
which begat Smalltalk-76, which begat Smalltalk-80. Smalltalk-80 was the basis for the 
commercial Smalltalks of the pre-Java era, VisualWorks Smalltalk (ParcPlaces commercial Smalltalk-80),
Smalltalk/V which merged with VisualWorks when ParcPlace and Digitalk merged, and &lt;span class="caps"&gt;IBM&lt;/span&gt;/VisualAge
Smalltalk. Squeak is also a child of Smalltalk-80.&lt;/p&gt;
&lt;p&gt;Smalltalk started to freeze when it became commercial.  We tried hard to forge a standard in
&lt;span class="caps"&gt;X3J20&lt;/span&gt; which allowed evolution and variation by underspecifying things as much as we could, but
broad enterprise acceptance slowed things down quite a bit.&lt;/p&gt;
&lt;p&gt;So it&amp;#8217;s exciting to see that Ruby is evolving. I hope that it too doesn&amp;#8217;t get bogged down
in it&amp;#8217;s success.&lt;/p&gt;
&lt;h2&gt;Personal Preferences&lt;/h2&gt;
&lt;p&gt;Finally getting back to my &amp;#8220;Coming from Smalltalk&amp;#8221; quote in ruby-talk, I expressed a personal
viewpoint that Ruby would be well-served if the mechanisms of singleton classes, and their use
as metaclasses were made visible and officially documented parts of the language.
If the rationale for not doing so is to preserve &amp;#8220;freedom-of-action&amp;#8221; in changing the
implementation in future versions, I can understand it, but to used a mixed language 
expression, that is a &amp;#8220;two-edged katana.&amp;#8221;  One of the things which Alan Kay told me a long time
ago was that he was disappointed that so few Smalltalkers experimented with changing and 
extending Smalltalk by building off of the mechanisms of Class, Metaclass, and Behavior.
In Ruby, despite the fact that knowledge of the equivalent implementation is only known 
&amp;#8220;sub-rosa,&amp;#8221; there is much more such experimentation.
Metaprogramming in Ruby is au courant, and lots of code is being written which depends on 
undocumented &amp;#8220;stuff.&amp;#8221;  I have to ask if this, being undocumented that is, is a good thing.&lt;/p&gt;
&lt;p&gt;So, to wrap this all up, while I do &amp;#8220;come from Smalltalk,&amp;#8221;  Today, I live in Ruby!&lt;/p&gt;</description>
      <pubDate>Thu, 21 Jun 2007 15:06:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e9e90ebe-9145-4ff6-977c-19478dc34a92</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/21/where-i-come-from</link>
      <category>psychology</category>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>humor</category>
      <category>language</category>
      <category>C</category>
      <category>java</category>
      <category>language_wars</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/434</trackback:ping>
    </item>
  </channel>
</rss>
