# Re: Point an element in Hash Object

**URL:** <https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/72745>\
**Category:** Ruby\
**Created:** [December 19, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/72745 "2005-12-19T23:12:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 19, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/72745/1 "2005-12-19T23:12:56Z")

</div>

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

You can’t serialize procs (well… you can, sort of, but it’s best not  
to think about it).

So, you’ll need to make a more specific Reference class:

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

Since this doesn’t store a proc, it can be serialized.

#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################  
#####################################################################################  
This e-mail message has been scanned for Viruses and Content and cleared  
by NetIQ MailMarshal  
#####################################################################################

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 19, 2005, 11:31pm UTC](https://www.ruby-forum.com/t/re-point-an-element-in-hash-object/72745/2 "2005-12-19T23:31:09Z")

</div>

Quoting Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> instance\_methods.each {|m| undef\_method m unless / **.\*** / === m}

Just as an aside, I’m wondering whether this is the best form  
of that particular idiom.

Probably what people _mean_ when they use / **.\*** / is “match any  
names _starting and ending_ with double underscores”, but that’s  
not what that regexp does – you’d need to write /^ **.\*** $/  
instead.

I usually stick with /^\_\_/ instead, as “starts with double  
underscores” seems to be sufficient in all interesting cases.

Other thoughts?

-mental
