Point an element in Hash Object

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"

···

-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Andrew
Sent: Thursday, 1 December 2005 9:06 AM
To: ruby-talk ML
Subject: Point an element in Hash Object

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.

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

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'

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

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.