Help me with some nested Hashes

What I want to do is have 2 nested hashes, the outer hash returning a new hash on an unknown key and the inner hash returning a space on an unknown key.

For example:
values = Hash.new # as described above
values[4][4] = "H" # The 4 is an unknown key, so a new Hash is formed # and the value of that inner hash's 4 key is "H"
values[5][6] # => " "

I got this to work correctly with the following (only ints are keys):
@value = []
0.upto(300) do |x|
  @value[x] = Hash.new(" ")
end
But that is horrible.

So I've tried this:
value = Hash.new(Hash.new(" "))
But if you pass an object as the default value for a hash, it is not cloned for each unknown key.

So then this:
value = Hash.new { Hash.new(" ") }
But that has some problems as well:
irb(main):013:0> value = Hash.new {Hash.new(" ") }
=> {}
irb(main):014:0> value[5][6] = "H"
=> "H"
irb(main):015:0> value[5][6]
=> " "
irb(main):016:0> value[5][7] = "I"
=> "I"
irb(main):017:0> value[5][6]
=> " " # It just changed from what I set it to above.

Is there anyway to get this to work?

Daniel Finnie wrote:

What I want to do is have 2 nested hashes, the outer hash returning a new hash on an unknown key and the inner hash returning a space on an unknown key.

value = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = " "}}

p value[5][6] # ==> " "
value[5][6] = "H"
p value # ==> {5=>{6=>"H"}}

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Daniel

I was beat to it, but since I spent ten minutes figuring it out, here is my solution anyway :slight_smile:

require 'rubygems'
require 'spec'

# a subclass, instead of a re-opened base class... how un-rubyish :wink:
class SpaceHash < Hash
   def (key)
     value = super(key)
     value = self[key] = Hash.new(" ") unless value
     value
   end
end

context "SpaceHash" do
   setup do
     @space_hash = SpaceHash.new
     @space_hash[5][6] = "H"
   end

   specify "unassigned keys return a space" do
     @space_hash[5][5].should == " "
   end

   specify "assigned keys should return their value" do
     @space_hash[5][6].should == "H"
   end

   specify "assigned another key to the second layer should not reassign othe keys" do
     @space_hash[5][7] = "I"
     @space_hash[5][6].should == "H"
   end
end

Ashley

···

On Dec 09, 2006, at 11:32 pm, Daniel Finnie wrote:

What I want to do is have 2 nested hashes, the outer hash returning a new hash on an unknown key and the inner hash returning a space on an unknown key.

Joel VanderWerf wrote:

Daniel Finnie wrote:

What I want to do is have 2 nested hashes, the outer hash returning a new hash on an unknown key and the inner hash returning a space on an unknown key.

value = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = " "}}

p value[5][6] # ==> " "
value[5][6] = "H"
p value # ==> {5=>{6=>"H"}}

Another option, in case you don't want all those empty strings cluttering up your hash:

value = Hash.new {|h,k| h[k] = Hash.new {" "}}

p value[5][6] # ==> " "
p value # ==> {5=>{}}

value[5][6] = "H"
p value # ==> {5=>{6=>"H"}}

# But, with this variation, beware:

value[5][7] << "add some characters after the space"
p value[5][7] # ==> " "

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks, that works perfectly! (I'm only storing 1 char strings)

Joel VanderWerf wrote:

···

Joel VanderWerf wrote:

Daniel Finnie wrote:

What I want to do is have 2 nested hashes, the outer hash returning a new hash on an unknown key and the inner hash returning a space on an unknown key.

value = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = " "}}

p value[5][6] # ==> " "
value[5][6] = "H"
p value # ==> {5=>{6=>"H"}}

Another option, in case you don't want all those empty strings cluttering up your hash:

value = Hash.new {|h,k| h[k] = Hash.new {" "}}

p value[5][6] # ==> " "
p value # ==> {5=>{}}

value[5][6] = "H"
p value # ==> {5=>{6=>"H"}}

# But, with this variation, beware:

value[5][7] << "add some characters after the space"
p value[5][7] # ==> " "