Hash pointing to reference of itself!

Discussion in 'Ruby' started by Peter Ehrlich, Apr 28, 2011.

  1. Hey!

    Supposed I want have a hash, with one key always holding the value of
    another key. Is this possible without referencing outside the hash
    closure? Forgive my lack of understanding of ruby internals.

    Cheers!

    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Ehrlich, Apr 28, 2011
    #1
    1. Advertising

  2. Peter Ehrlich

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Wed, Apr 27, 2011 at 9:06 PM, Peter Ehrlich <>wrote:

    > Hey!
    >
    > Supposed I want have a hash, with one key always holding the value of
    > another key. Is this possible without referencing outside the hash
    > closure? Forgive my lack of understanding of ruby internals.
    >
    > Cheers!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    You can't do that, that would require variables being able to point to other
    variables.

    You could, however, fake it by creating your own hash which allows this
    functionality, as shown below. It just has an internal hash that allows keys
    to refer to other keys, then when you request one, it follows the chain of
    referrals until it arrives at an end point, and gives you back that key.
    While this is a bit interesting, it is more likely that whatever you are
    trying to do is not the Ruby way of solving your problem. Are you coming
    from a language like C++ where you have direct access to pointers and
    references? You might be trying to write Ruby like it is that other
    language. Perhaps if you tell us more about your use case, we can offer a
    solution that is better than "this technically does what you are
    requesting".



    class RHash

    def initialize
    @data = Hash.new
    @references = Hash.new
    end

    def [](key)
    return nil unless in_reference_chain? key
    @data[actual key]
    end

    def []=(key,value)
    @data[actual key] = value
    end

    def refer(references)
    references.each do |from, to|
    reference_chain from, to
    @data.delete from if orphaned? from
    end
    end

    private

    def orphaned?(from)
    @references[from] != from
    end

    def in_reference_chain?(key)
    @references.has_key? key
    end

    def reference_chain(from, to=from)
    @references[from] = to
    end

    def actual(key)
    reference_chain key unless in_reference_chain? key
    referenced = @references[key]
    return referenced if referenced == key
    return actual referenced
    end
    end


    h = RHash.new
    h[:foo] # => nil
    h[:foo] = 5
    h[:foo] # => 5

    h[:bar] # => nil
    h.refer :bar => :foo
    h[:bar] # => 5
    h[:foo] = 6
    h[:bar] # => 6


    h # => #<RHash:0x00000100852b58 @data={:foo=>6}, @references={:foo=>:foo,
    :bar=>:foo}>
    h.refer :foo => :baz
    h # => #<RHash:0x00000100852b58 @data={}, @references={:foo=>:baz,
    :bar=>:foo}>

    h[:baz] = 12
    h[:foo] # => 12
    h[:bar] # => 12
    h[:baz] # => 12

    h[:foo] = 40
    h[:foo] # => 40
    h[:bar] # => 40
    h[:baz] # => 40

    h.refer :foo => :foo
    h # => #<RHash:0x00000100852b58 @data={:baz=>40}, @references={:foo=>:foo,
    :bar=>:foo, :baz=>:baz}>
    h[:foo] # => nil
    h[:bar] # => nil
    h[:baz] # => 40

    h["garlic"] = "vampire"
    h.refer :foo => 'garlic', :bar => :baz
    h[:foo] # => "vampire"
    h['garlic'] # => "vampire"
    h[:bar] # => 40
    h[:baz] # => 40


    note: if you decide you actually want to use this code, let me know, and
    I'll give you the "test suite" I used while writing it.
     
    Josh Cheek, Apr 28, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    1
    Views:
    378
    David Carlisle
    Apr 26, 2005
  2. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    676
    David A. Black
    Jul 2, 2008
  4. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    144
    Steve
    Sep 28, 2003
  5. Replies:
    14
    Views:
    271
    Tomi Häsä
    Jan 10, 2005
Loading...

Share This Page