Re: Point an element in Hash Object


#1

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


#2

Quoting Daniel S. 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