Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3594 > unrolled thread
| Started by | Peter Ehrlich <crazedcougar@gmail.com> |
|---|---|
| First post | 2011-04-27 21:06 -0500 |
| Last post | 2011-04-28 00:03 -0500 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.ruby
Hash pointing to reference of itself! Peter Ehrlich <crazedcougar@gmail.com> - 2011-04-27 21:06 -0500
Re: Hash pointing to reference of itself! Josh Cheek <josh.cheek@gmail.com> - 2011-04-28 00:03 -0500
| From | Peter Ehrlich <crazedcougar@gmail.com> |
|---|---|
| Date | 2011-04-27 21:06 -0500 |
| Subject | Hash pointing to reference of itself! |
| Message-ID | <2c4044cb0ff545728e716d86ae47c93d@ruby-forum.com> |
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/.
[toc] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-28 00:03 -0500 |
| Message-ID | <BANLkTindhNS=QfUCNZkSMVuSSRg2FYo67Q@mail.gmail.com> |
| In reply to | #3594 |
[Note: parts of this message were removed to make it a legal post.]
On Wed, Apr 27, 2011 at 9:06 PM, Peter Ehrlich <crazedcougar@gmail.com>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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web