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.
daniels (Guest)
on 2005-12-01 03:17
(Received via mailing list)
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.
#####################################################################################
lazax.com (Guest)
on 2005-12-01 03:21
(Received via mailing list)
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'
Andrew <> (Guest)
on 2005-12-01 15:11
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.
> #####################################################################################
Andrea (Guest)
on 2005-12-19 18:39
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
This topic is locked and can not be replied to.