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 = '[EMAIL PROTECTED]'
  => "[EMAIL PROTECTED]"
  irb> p
  => #<Person:0x2bc43d0 @personal_email="[EMAIL PROTECTED]", 
@field_list=[:first_name, :last_name, :middle_name, :location, 
:primary_email, :primary_phone_number, :personal_email]>
  irb> p.primary_email = p.personal_email
  => "[EMAIL PROTECTED]"
  irb> p
  => #<Person:0x2bc43d0 @personal_email="[EMAIL PROTECTED]", 
@primary_email="[EMAIL PROTECTED]", @field_list=[:first_name, :last_name, 
:middle_name, :location, :primary_email, :primary_phone_number, 
:personal_email]>
  irb> p.personal_email.replace '[EMAIL PROTECTED]'
  => "[EMAIL PROTECTED]"
  irb> p
  => #<Person:0x2bc43d0 @personal_email="[EMAIL PROTECTED]", 
@primary_email="[EMAIL PROTECTED]", @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 '[EMAIL PROTECTED]'" can be replaced with 
"p.personal_email = '[EMAIL PROTECTED]'" 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
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to