# Re: Point an element in Hash Object

**URL:** <https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358>\
**Category:** Ruby\
**Created:** [December 1, 2005, 2:17am UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358 "2005-12-01T02:17:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 1, 2005, 2:17am UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358/1 "2005-12-01T02:17:45Z")

</div>

I thought something like this was in facets… maybe it was one of those  
other library collections… either way I can’t find what I’m looking  
for, so here’s an implementation which probably screws up with some  
corner cases.

class Reference  
instance\_methods.each { |m| undef\_method m unless / **.\*** / === m}  
def initialize(&block)  
@block = block  
end  
def method\_missing(\*args, &block)  
@block.call.send(\*args, &block)  
end  
end

my\_hash = {}  
my\_hash[“one”] = “First”  
my\_hash[“two”] = “Second”  
my\_hash[“three”] = “Third”  
my\_hash[“four”] = Reference.new{my\_hash[“one”]}  
my\_hash[“five”] = Reference.new{my\_hash[“four”]}

my\_hash[“one”] = “Modify”  
p my\_hash[“four”]  
=\> “Modify”  
p my\_hash[“five”]  
=\> “Modify”

> element of hash object can point to another element. But i  
> need the link to the key and not a copy of the value.

#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 1, 2005, 2:21am UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358/2 "2005-12-01T02:21:48Z")

</div>

Just a note about previous solution: referencing a reference does  
daisy-chained lookups. So in the above example  
my\_hash[“file”].to\_s  
will send “to\_s” my\_hash[“four”], which will then forward that message  
to my\_hash[“one”].  
So it would be more efficient (computationally) to reference directly,  
and not indirectly. This is not the same as pointers in C/C++!

Also, recursive references will overflow the stack:

my\_hash[“six”] = “Sixth”  
my\_hash[“six”] = Reference.new{my\_hash[“six”]}  
SystemStackError: stack level too deep  
from (irb):40:in `method\_missing’

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 1, 2005, 2:11pm UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358/3 "2005-12-01T14:11:29Z")

</div>

Thank too much for your help.  
This is what i need…

Andrew

daniels wrote:

> I thought something like this was in facets… maybe it was one of those  
> other library collections… either way I can’t find what I’m looking  
> for, so here’s an implementation which probably screws up with some  
> corner cases.
> 
> class Reference  
> instance\_methods.each { |m| undef\_method m unless / **.\*** / === m}  
> def initialize(&block)  
> @block = block  
> end  
> def method\_missing(\*args, &block)  
> @block.call.send(\*args, &block)  
> end  
> end
> 
> my\_hash = {}  
> my\_hash[“one”] = “First”  
> my\_hash[“two”] = “Second”  
> my\_hash[“three”] = “Third”  
> my\_hash[“four”] = Reference.new{my\_hash[“one”]}  
> my\_hash[“five”] = Reference.new{my\_hash[“four”]}
> 
> my\_hash[“one”] = “Modify”  
> p my\_hash[“four”]  
> =\> “Modify”  
> p my\_hash[“five”]  
> =\> “Modify”
> 
> > element of hash object can point to another element. But i  
> > need the link to the key and not a copy of the value.
> 
> #####################################################################################  
> This email has been scanned by MailMarshal, an email content filter.  
> #####################################################################################

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 19, 2005, 5:39pm UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/51358/4 "2005-12-19T17:39:46Z")

</div>

daniels wrote:

> I thought something like this was in facets… maybe it was one of those  
> other library collections… either way I can’t find what I’m looking  
> for, so here’s an implementation which probably screws up with some  
> corner cases.
> 
> class Reference  
> instance\_methods.each { |m| undef\_method m unless / **.\*** / === m}  
> def initialize(&block)  
> @block = block  
> end  
> def method\_missing(\*args, &block)  
> @block.call.send(\*args, &block)  
> end  
> end
> 
> my\_hash = {}  
> my\_hash[“one”] = “First”  
> my\_hash[“two”] = “Second”  
> my\_hash[“three”] = “Third”  
> my\_hash[“four”] = Reference.new{my\_hash[“one”]}  
> my\_hash[“five”] = Reference.new{my\_hash[“four”]}
> 
> my\_hash[“one”] = “Modify”  
> p my\_hash[“four”]  
> =\> “Modify”  
> p my\_hash[“five”]  
> =\> “Modify”
> 
> > element of hash object can point to another element. But i  
> > need the link to the key and not a copy of the value.

This is ok… but i have a question.  
If i dump the hash into a YAML file, then when i load it, the reference  
is lost and there are only copies of the same values.

How can I mantain that reference when I dump/load the YAML file?

Thank so mach to all for the help.  
–Andrea
