pass by reference or pass by value

I got it, I go it, I got it!!!!!!!!

def change str     str.replace '...' end

str = '.' change str puts str

#=> ...

# yey!

this should be inherited from kernel/Object and then overriden so a clear interface can be followed, my personal belief.

Kazuyoshi Tlacaelel wrote:

I got it, I go it, I got it!!!!!!!!

def change str     str.replace '...' end

str = '.' change str puts str

#=> ...

# yey!

(Hey guys, I realize this thread is a month old, but its relevant to something I'm doing right now.)

Wow, this is the first time I've been disappointed by Ruby. This seems to me to be a _very_ poor choice. This (real code)...

  string = 'a string'   copy = string   string.replace 'a different string'

...seems a very poor substitute for this (pseudocode), which I would expect to have the same result...

  string = 'a string'   copy = *string   string = 'a different string'

...because the real code requires the changer of the string to know that there is going to be a copy made of the string. Here's a more realistic example (assume Person is a class that allows new instance variables to be created at runtime; in fact, I have completely implemented such a Person class and the irb I/O shown is completely real):

  > p = Person.new   => #<Person:0x2bc43d0 @field_list=[:first_name, :last_name, :middle_name, :location, :primary_email, :primary_phone_number]>   > p.personal_email = 'me@home.com'   => "me@home.com"   > p   => #<Person:0x2bc43d0 @personal_email="me@home.com", @field_list=[:first_name, :last_name, :middle_name, :location, :primary_email, :primary_phone_number, :personal_email]>   > p.primary_email = p.personal_email   => "me@home.com"   > p   => #<Person:0x2bc43d0 @personal_email="me@home.com", @primary_email="me@home.com", @field_list=[:first_name, :last_name, :middle_name, :location, :primary_email, :primary_phone_number, :personal_email]>   > p.personal_email.replace 'me@gmail.com'   => "me@gmail.com"   > p   => #<Person:0x2bc43d0 @personal_email="me@gmail.com", @primary_email="me@gmail.com", @field_list=[:first_name, :last_name, :middle_name, :location, :primary_email, :primary_phone_number, :personal_email]>

Is there really no way to do this such that the line "p.personal_email.replace 'me@gmail.com'" can be replaced with "p.personal_email = 'me@gmail.com'" and both strings will be modified? I think that's terrible--obviously, it's pretty realistic to expect that code like this would occur, but making it possible it requires every piece of code that changes p.personal_email to know that there is or could be a copy of it made. It makes String.= virtually useless, because in order to make it possible for a user to create a new String that always reflects the contents of another String, one must always use String.replace instead of String.= to assign values to Strings. It seems like the best solution would simply be to add an =* operator (to Object itself, ideally) such that this would be possible, in place of the line "p.primary_email = p.personal_email" above:

  p.primary_email =* p.personal_email #p.primary_email now contains a pointer directly to p.personal_email instead of containing a pointer to the same object that p.personal_email points to

Of course, that is easier said than done, but I can't think of any other solution that comes close to being as elegant.

Am I missing something? Is there already a way to create pointers to pointers in Ruby so that this problem can be avoided?

Thanks, Doug

string = 'a string' copy = string string.replace 'a different string'

...because the real code requires the changer of the string to know that there is going to be a copy made of the string.

For me, Object#clone or Object#dup works:

string = 'a string' copy = string.clone string = 'a different string'