Forum: Ruby Re: Point an element in Hash Object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 Daniel Sheppard (Guest)
on 2005-12-19 23:12
(Received via mailing list)
> 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
#####################################################################################
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2005-12-19 23:31
(Received via mailing list)
Quoting Daniel Sheppard <daniels@pronto.com.au>:

> 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
This topic is locked and can not be replied to.