On Aug 6, 2007, at 3:28 PM, dblack / rubypal.com wrote:

> I'd agree about the literal 1 in comparison with other literals:
>
>   1        # textual representation of that object
>   "hi"     # textual representation of that object
>   [1,2,3]  # etc.
>

In the way I think about things those examples are not the same.

An integer literal is a representation of a constant
reference to a particular object.  It doesn't matter
how many times the literal appears in the text, it will always
refer to a particular object, in this case the Fixnum instance that
behaves like the integer 1.  There is no object creation involved
when an expression like 1 or 42 is encountered by Ruby unlike your
second two examples.

"hi" is special syntax for constructing a brand new
instance of String and [1,2,3] is a special syntax for constructing
a brand new instance of Array.

   (1..10).map { 1 }     # array has 10 identical references
   (1..10).map { "hi" }  # array has 10 different references


> but if you do this:
>
>   a = 1
>   b = "hi"
>
> now a (as I understand it) is actually bound to the integer 1, while b
> is bound to a reference to the object.  (I can't say "A reference to
> 'hi'" since that 'hi' would be a different one.... :-)
>

The way I think about this is that

   a = 1

binds a to the reference for the Fixnum instance 1. And in

   b = "hi"

the right hand side causes a new instance of String to be  
instantiated and
the reference to that new instance is bound to b.

In both cases, the rhs evaluates to a reference, which is bound to a
variable.  I like that symmetry.

Gary Wright