On Jan 6, 2006, at 8:21 AM, Jim Weirich wrote:
> Dave Howell wrote:
>> A variable is a box.
>
> Hmmm ... see http://onestepback.org/index.cgi/Tech/Ruby/Shoeboxes.rdoc
> for an alternate view.  I think the mental model of "boxes" as  
> variables
> doesn't work so well for Ruby.

Good point in your article - shoeboxes (or pigeonholes, as I  
originally learned variables) are bad metaphors in a dynamically- 
typed language.

Saying "bound" or "named" is right, but doesn't provide the same  
visual mental model for beginners.

I like to explain it that variables are nametags that you hang on  
things.

Assignment places a nametag on an object.
	person = 'Gavin'
	# Create a new box with the letters 'Gavin' in it;
	# hang on it nametag with 'person' scrawled on it

	me = person
	# Find the box with the 'person' nametag on it,
	# hang another nametag on it with 'me' scrawled thereon

	person = nil
	# Take the person nametag off the box,
	# and put it in the big black hole of tags without boxes

	me = nil
	# Do the same thing with the me nametag

Periodically, the Cosmic Janitor comes through with his rolling  
garbage can. Any boxes that don't have name tags get thrown away,  
leaving more room on the floor for you.

This metaphor makes it a little bit confusing explaining Arrays and  
Hashes to new users of a dynamic language. At that point, I usually  
ditch the nametag metaphor and start using fishing line that goes  
between the nametag and the boxes. Arrays, then, are boxes with a  
bunch of nails on them, with 0..n drawn on the heads. The fishing  
line gets tied between a nail and a certain box, and the Janitor's  
broom sweeps away anything that isn't tied to something else.

The benefit of the fishing line example is that it also helps with  
scope. When I'm standing in one spot of the room, I can only see the  
nametags on the floor near me, even though the fishing line on them  
runs over to another box.