Odd : a = Hash.new(Hash.new)

irb(main):086:0> a = Hash.new(Hash.new)
=> {}
irb(main):087:0> a[5]
=> {}
irb(main):088:0> a[5][4] = 3
=> 3
irb(main):089:0> a
=> {}
irb(main):090:0> a[5]
=> {4=>3}
irb(main):091:0> a[5][4]
=> 3
irb(main):092:0> RUBY_DESCRIPTION
=> “ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]”

Upon inspection of ‘a’, it sure seems that it’s empty, but upon
inspection of the key, it does have a value attached to it! Can we trust
this? Expected / unexpected behavior ?

Aldric G. wrote:

Upon inspection of ‘a’, it sure seems that it’s empty, but upon
inspection of the key, it does have a value attached to it! Can we trust
this? Expected / unexpected behavior ?

Reminder : a = Hash.new(Hash.new)

irb(main):097:0> a[6] ||= {4 => 4}
=> {4=>3}
irb(main):098:0> a[6]
=> {4=>3}
irb(main):099:0> a
=> {}
irb(main):100:0> a[6]
=> {4=>3}
irb(main):101:0> a[5]
=> {4=>3}

Just for kicks.

On Wed, Nov 4, 2009 at 9:22 PM, Aldric G. [email protected]
wrote:

Aldric G. wrote:

Upon inspection of ‘a’, it sure seems that it’s empty, but upon
inspection of the key, it does have a value attached to it! Can we trust
this? Expected / unexpected behavior ?

Reminder : a = Hash.new(Hash.new)

The default argument to a hash gets returned when the key is not
found, but not added to the hash. The logic is more or less

def
if self.contains(key):
return self.value_of(key)
else
return default
end

What you want is the block version, which accepts the hash itself and
the key as parameters, and lets you do the right thing, here

a = Hash.new {|hash, key| hash[key] = Hash.new}

a = Hash.new {|hash, key| hash[key] = Hash.new}
=> {}
a[5]
=> {}
a
=> {5=>{}}

martin

On Nov 4, 9:48 am, Aldric G. [email protected] wrote:

irb(main):091:0> a[5][4]
=> 3
irb(main):092:0> RUBY_DESCRIPTION
=> “ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]”

Upon inspection of ‘a’, it sure seems that it’s empty, but upon
inspection of the key, it does have a value attached to it! Can we trust
this? Expected / unexpected behavior ?

Maybe this might explain the behavior a bit more clearly:

>> d = Hash.new
=> {}
>> a = Hash.new(d)
=> {}
>> a[5][4] = 3
=> 3
>> a
=> {}
>> a[5]
=> {4=>3}
>> a[6]
=> {4=>3}
>> d
=> {4=>3}

Notice that the default value for hash a (the other hash) has changed.
And you’ve already noticed that the hash just appears to have values
when queried specifically, but not when inspected.

You probably want to declare the hash as Hash.new { |h, k| h[k] =
{} }

Also, see this if you want autovivification all the way down:

martin

Robert K. wrote:

IMHO this solution is even simpler because it does not need the method:

h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}

Robert - I agree. I like this one better. Feels a little more ruby-ish.
Is there a way to use Proc (or whatever does work) to save this block
and reuse it when creating a new hash ?
Something like :
a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
b = Hash.new a

Note, I know this is wrong… It’ll just put the proc as the default
value, instead of triggering the proc.

On Wed, Nov 4, 2009 at 6:10 PM, Aldric G. [email protected]
wrote:

a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
b = Hash.new a

Note, I know this is wrong… It’ll just put the proc as the default
value, instead of triggering the proc.

irb(main):001:0> a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
=> #Proc:0xb7d81730@:1(irb)
irb(main):002:0> b = Hash.new &a
=> {}
irb(main):003:0> b[1][2] = 3
=> 3
irb(main):004:0> b
=> {1=>{2=>3}}

Jesus.

Jesús Gabriel y Galán wrote:

irb(main):001:0> a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
=> #Proc:0xb7d81730@:1(irb)
irb(main):002:0> b = Hash.new &a
=> {}
irb(main):003:0> b[1][2] = 3
=> 3
irb(main):004:0> b
=> {1=>{2=>3}}

Jesus.

Have a +1 ! I’m not forgetting THAT lesson.

2009/11/4 Martin DeMello [email protected]:

Also, see this if you want autovivification all the way down:

taw's blog: Autovivification in Ruby

IMHO this solution is even simpler because it does not need the method:

h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}

irb(main):002:0> h[1][2][3]=4
=> 4
irb(main):003:0> h
=> {1=>{2=>{3=>4}}}

Kind regards

robert

On 11/04/2009 06:10 PM, Aldric G. wrote:

Robert K. wrote:

IMHO this solution is even simpler because it does not need the method:

h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}

Robert - I agree. I like this one better. Feels a little more ruby-ish.
Is there a way to use Proc (or whatever does work) to save this block
and reuse it when creating a new hash ?

The block is saved and reused when creating nested Hashes!

Something like :
a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
b = Hash.new a

Note, I know this is wrong… It’ll just put the proc as the default
value, instead of triggering the proc.

No need for that. Please look at my code again.

Kind regards

robert

2009/11/5 Jesús Gabriel y Galán [email protected]:

Robert - I agree. I like this one better. Feels a little more ruby-ish.
instead of triggering the proc.
Maybe this is his use case?
If I wanted to save typing I’d place the whole construction in a method

def cnh # silly name “create nested hash”
Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
end

a = cnh
b = cnh
c = cnh
d = cnh

Kind regards

robert

On Thu, Nov 5, 2009 at 8:25 AM, Robert K.
[email protected] wrote:

reuse it when creating a new hash ?
No need for that. Please look at my code again.
I think he might want to save typing:

a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
b = Hash.new &a
c = Hash.new &a
d = Hash.new &a

Maybe this is his use case?

Jesus.

Robert K. wrote:

2009/11/5 Jes�s Gabriel y Gal�n [email protected]:

Robert - I agree. I like this one better. Feels a little more ruby-ish.
instead of triggering the proc.
Maybe this is his use case?
If I wanted to save typing I’d place the whole construction in a method

def cnh # silly name “create nested hash”
Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
end

a = cnh
b = cnh
c = cnh
d = cnh

Kind regards

robert

Oh sure - be lazier than me. I see how it is. :slight_smile:
Thanks - Jesus was right… I was trying to be lazy.