Re: pass by reference or pass by value

8 views
Skip to first unread message

Kazuyoshi Tlacaelel

unread,
Apr 8, 2008, 9:23:29 AM4/8/08
to rubyonra...@googlegroups.com

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


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

str = '.'
change str
puts str

#=> ...

# yey!
--
Posted via http://www.ruby-forum.com/.

Kazuyoshi Tlacaelel

unread,
Apr 8, 2008, 9:27:47 AM4/8/08
to rubyonra...@googlegroups.com
this should be inherited from kernel/Object and then overriden
so a clear interface can be followed, my personal belief.

Doug Glidden

unread,
Sep 8, 2008, 9:29:19 AM9/8/08
to rubyonra...@googlegroups.com
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):

irb> p = Person.new
=> #<Person:0x2bc43d0 @field_list=[:first_name, :last_name,
:middle_name, :location, :primary_email, :primary_phone_number]>
irb> p.personal_email = 'm...@home.com'
=> "m...@home.com"
irb> p
=> #<Person:0x2bc43d0 @personal_email="m...@home.com",
@field_list=[:first_name, :last_name, :middle_name, :location,
:primary_email, :primary_phone_number, :personal_email]>
irb> p.primary_email = p.personal_email
=> "m...@home.com"
irb> p
=> #<Person:0x2bc43d0 @personal_email="m...@home.com",
@primary_email="m...@home.com", @field_list=[:first_name, :last_name,
:middle_name, :location, :primary_email, :primary_phone_number,
:personal_email]>
irb> p.personal_email.replace 'm...@gmail.com'
=> "m...@gmail.com"
irb> p
=> #<Person:0x2bc43d0 @personal_email="m...@gmail.com",
@primary_email="m...@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 'm...@gmail.com'" can be replaced with
"p.personal_email = 'm...@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

Zachary Smith

unread,
Jan 10, 2015, 5:06:35 AM1/10/15
to rubyonra...@googlegroups.com
>> 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'

Reply all
Reply to author
Forward
0 new messages