# Point an element in Hash Object

**URL:** <https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178>\
**Category:** Ruby\
**Created:** [November 30, 2005, 11:05pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178 "2005-11-30T23:05:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [November 30, 2005, 11:05pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/1 "2005-11-30T23:05:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [November 30, 2005, 11:44pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/2 "2005-11-30T23:44:16Z")

</div>

On Wed, 30 Nov 2005 22:05:53 -0000, Andrew [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @my\_hash[“four”] = @my\_hash[“T1”]
> 
> 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 😉

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [December 1, 2005, 12:40pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/3 "2005-12-01T12:40:50Z")

</div>

Ross B. a Ã©crit :

> > @my\_hash[“one”] = “First”  
> > The problem is that i woult like to refer the element end not to copy  
> > ;)). 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 😉

–  
Lionel T.

Personal web site: [http://users.skynet.be/lthiry/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [December 1, 2005, 2:22pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/4 "2005-12-01T14:22:52Z")

</div>

Hi –

On Thu, 1 Dec 2005, Ross B. wrote:

> (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

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [December 1, 2005, 2:18pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/5 "2005-12-01T14:18:48Z")

</div>

On Thu, 01 Dec 2005 11:33:36 -0000, Lionel T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > @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 ‘!’ ?)

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [December 1, 2005, 2:35pm UTC](https://www.ruby-forum.com/t/point-an-element-in-hash-object/49178/6 "2005-12-01T14:35:05Z")

</div>

On Thu, 01 Dec 2005 13:21:27 -0000, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

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.
