Dave Anderson wrote:
> Rick Denatale wrote:
> ...
>> Objects are instances of classes, and typically have state, in some
>> cases like Fixnums the only state is really the object's identity, and
>> such objects are immutable, if two such objects have different state,
>> then they MUST be different objects.  There is only one 1 object and
>> one 5 object.  Symbols are similar in that there is only one instance
>> of symbol with a particular value.
> 
> Thanks for the fascinating and valuable insights, Rick. I didn't realize 
> what a can of worms I was opening here. And it's nice to know that 
> someone here remembers Fortran II, although that might make you a good 
> candidate for a Men-In-Black neurolizer. ;-) As an old hand at C++, this 
> variable/value/type stuff is a very worthwhile lesson.
> 
> One practical thing I can draw out of this is the difference between 
> these two functions:
> 
>   def X_1( str )
>     str[0] = 'X'
>   end
> 
>   def X_2( str )
>      str = 'X' + str[ 1..-1 ]
>   end
> 
> The X_1 function alters the passed-in object in place, 

Remember that in ruby "[]=" is the name of a method:

> str[0] = 'X'

And a method can be programmed to alter the original string and return 
it; or it can create a new string that is a copy of the original and 
return the altered copy.

> I don't recall reading this in the literature anywhere.

$ri String#[]=
------------------------------------------------------------- String#[]=
     str[fixnum] = fixnum
     str[fixnum] = new_str
     str[fixnum, fixnum] = new_str
     str[range] = aString
     str[regexp] = new_str
     str[regexp, fixnum] = new_str
     str[other_str] = new_str
------------------------------------------------------------------------
 Element Assignment---***Replaces some or all of the content of _str_ 
(that should read 'str' in my opinion).***


> while the X_2 
> function modifies a local copy. 

With a basic assignment statement like this:

> str = 'X' + str[ 1..-1 ]

'=' is not the name of a String method.  Instead:

---
This form of assignment is hardwired into the language.
---
(pickaxe2, p. 90)


And that type of assignment in ruby works like this:

If you write this:

x = 10

you get this:

x --> 10

In other words, x refers to 10.  If you then write:

y = 10

you get:

x-----> 10
        ^
        |
y ------+


Then if you write:

y = 20

you do not get:

x-----> 20
        ^
        |
y ------+

Instead you get:

x ----> 10

y ----> 20

I like to think of it like this: with that basic type of assignment you 
take the variable name on the left of the assignment, 'y' in this case, 
and write it on a stick-it note, and paste the stick-it note on the 
object on the right, the object 20.  If ruby has already written the 
same variable name on a stick-it note and pasted it on some other object 
prior to your assignment statement(in the same scope), then ruby tears 
the stick-it note off the other object and pastes it on the object 20.


-- 
Posted via http://www.ruby-forum.com/.