Point an element in Hash Object

A

Andrew

I'm trying to create an hash object using object string for key and
value.
I need to link a key with another key as value... so an element of hash
object can point to another element. But i need the link to the key and
not a copy of the value.

I have tried in this way:
@my_hash["one"] = "First"
@my_hash["two"] = "Second"
@my_hash["three"] = "Third"
@my_hash["four"] = @my_hash["T1"]

Now if I modify
@my_hash["one"] = "Modify"
I would like to see the new value also for
@my_hash["four"] => "Modify"
but I see "First" because i think it do a copy.

The problem is that i woult like to refer the element end not to copy
it.
Thanks so much and sorry for my english.

Andreaw
 
R

Ross Bamford

I'm trying to create an hash object using object string for key and
value.
I need to link a key with another key as value... so an element of hash
object can point to another element. But i need the link to the key and
not a copy of the value.

I have tried in this way:
@my_hash["one"] = "First"
@my_hash["two"] = "Second"
@my_hash["three"] = "Third"
@my_hash["four"] = @my_hash["T1"]

Now if I modify
@my_hash["one"] = "Modify"
I would like to see the new value also for
@my_hash["four"] => "Modify"
but I see "First" because i think it do a copy.

The problem is that i woult like to refer the element end not to copy
it.
Thanks so much and sorry for my english.

Andreaw

(Caveat: Fairly new to Ruby)

You actually do have a reference - AFAIU so far, pretty much everything is
passed by reference (except references, which are passed by value ;)).
Prior to your '@my_hash["one"] = "Modify"' they do both reference the same
object, but then you replace the reference with a new reference, to a
string "Modify".

Swap

@my_hash["one"] = "Modify"

for

@my_hash["one"].sub!(/First/,'Modify')

and you should get the result you want, because 'sub!' modifies the
receiver, so no new reference is assigned to @my_hash["one"].

(N.B. that this just illustrates the problem more, it's not a general
solution. For that I'd probably use a holder for the string (maybe an
one-element array), but I don't fully know what Ruby has to offer instead
yet ;)
 
L

Lionel Thiry

Ross Bamford a écrit :
I'm trying to create an hash object using object string for key and
value.
I need to link a key with another key as value... so an element of hash
object can point to another element. But i need the link to the key and
not a copy of the value.

I have tried in this way:
@my_hash["one"] = "First"
@my_hash["two"] = "Second"
@my_hash["three"] = "Third"
@my_hash["four"] = @my_hash["T1"]

Now if I modify
@my_hash["one"] = "Modify"
I would like to see the new value also for
@my_hash["four"] => "Modify"
but I see "First" because i think it do a copy.

The problem is that i woult like to refer the element end not to copy
it.
Thanks so much and sorry for my english.

Andreaw

(Caveat: Fairly new to Ruby)

You actually do have a reference - AFAIU so far, pretty much everything
is passed by reference (except references, which are passed by value
;)). Prior to your '@my_hash["one"] = "Modify"' they do both reference
the same object, but then you replace the reference with a new
reference, to a string "Modify".

Swap

@my_hash["one"] = "Modify"

for

@my_hash["one"].sub!(/First/,'Modify')

@my_hash["one"].replace 'Modify'
is better
and you should get the result you want, because 'sub!' modifies the
receiver, so no new reference is assigned to @my_hash["one"].

(N.B. that this just illustrates the problem more, it's not a general
solution. For that I'd probably use a holder for the string (maybe an
one-element array), but I don't fully know what Ruby has to offer
instead yet ;)
 
D

David A. Black

Hi --

@my_hash["one"].sub!(/First/,'Modify')

@my_hash["one"].replace 'Modify'
is better

Definitely. Thanks. :)

(P.s. is 'replace' one of the '!' convention exceptions, or is there another
reason it doesn't have a '!' ?)

It's not an exception: the convention is that when there's a pair of
methods that differ only in that one is more "dangerous" than the
other, they have the same name but the dangerous one has a ! on the
end. replace isn't part of a pair of that kind -- it's just its own
thing.


David
 
R

Ross Bamford

It's not an exception: the convention is that when there's a pair of
methods that differ only in that one is more "dangerous" than the
other, they have the same name but the dangerous one has a ! on the
end. replace isn't part of a pair of that kind -- it's just its own
thing.

Ahh, I see... I had the idea that the bang signified a method that
modified self, regardless. A few other 'exceptions' make sense to me now,
too :)

Thanks David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,642
Latest member
arunkumar99

Latest Threads

Top