On Thu, Jan 17, 2002 at 04:55:46AM +0900, Phil Tomson wrote:
> 
> Jim,
> 
> What exactly are you trying to accomplish?  Maybe there's a different way 
> to do it, perhaps with Structs or with  a hash of hashes?
> 

Hmmm..well, not being the XML expert, I am somewhat afraid
to detail what I am doing. :)

<ducking behind desk>
  I have built an abstract xml tree class. The class
  uses NQXML and creates a tree structure with nodes,
  attributes and data from the xml file. Comments are ignored.
  The motivation for doing this was to have
  easier access to the xml data instead of the NQXML
  notation. EG, I can write
    name = axt.entity['name']
  instead of
    name = node.children[0].entity.to_s
  Which I could never remember.
</ducking behind desk>

  As far as a solution, I think I have one that is suitable.
  However, I have not tried Structs. I have never used them.

  What I did was prevent returning a pointer to the hash.
  Instead, if the user asks for the hash, they get their own copy.
  If they ask for the value given a key, I return that value.

  class Test
    def initialize
      @h = {'a'=>"fred"}
    end
    def h(k=nil)
      return Marshal.load(Marshal.dump(@h)) if k.nil?
      @h[k]
    end
  end

  t = Test.new
  t.h  #=> I get my own copy of the hash
  t.h(key) #=> returns @h[key]
  t.h[key] #=> makes a copy of the hash and returns @h[key]
  t.h[key] = 5  #=> makes a copy of the hash and sets key. Does not affect @h
  t.h(key) = 5  #=> syntax error


-- 
Jim Freeze
Today is a fine day for Ruby programming.
www.freeze.org