Topic: making one variable equal to another

Given Ruby's odd (to me anyway) way of using variables to point to values instead of holding them, what is the best way to load one variable with the value in another so that the new variable does not change in synch with the other?  Rails loves to discard things that I want to keep - and the only method i have come across is:

@order.ship_to = @customer.addr.dup

where (hopefully) @order.ship_to does not reset to nil when the customer instance variables are trashed by Rails...

I guess Ruby was designed this way for a reason - but I cannot see why you would want one variable to have several names.  Why not use the same variable name throughout? Under what circumstances is this useful?

Re: making one variable equal to another

It's useful to save on memory and because if the two objects really are the same thing it's pretty handy to be able to update one and the other is updated as well.  You can always use the dup method when you need to.

Re: making one variable equal to another

save on memory?  why not save on typing and use the same variable name all the time?
i can't figure out why anyone would want two names to describe the same thing.
but thanks for the confirmation of .dup - its counterintuitive, but im getting used to that!

Re: making one variable equal to another

SydneyStephen wrote:

its counterintuitive, but im getting used to that!

It's a trade off because creating a copy of an object every time you pass it as a parameter to a method would degrade performance. Passing objects by reference doesn't seem strange at all; every other language is doing it, even the big and fancy ones. (Java and C# come to mind).

Re: making one variable equal to another

Are you saying that "pass" in def method(pass) would otherwise require another copy of the variable "password" in the statement (call) method(password)?

I guess it would but in other languages you would pass the variable using the same name and no copy would be made. 

I still can't see any value in the variable password having a zillion different names - why not just use the same name always?