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 ?

···

--
Posted via http://www.ruby-forum.com/.

Aldric Giacomoni 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.

···

--
Posted via http://www.ruby-forum.com/\.

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 ?

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] =
{} }

···

On Nov 4, 9:48 am, Aldric Giacomoni <ald...@trevoke.net> wrote:

--
-yossef

Aldric Giacomoni 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 (key)
  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 Wed, Nov 4, 2009 at 9:22 PM, Aldric Giacomoni <aldric@trevoke.net> wrote:

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

martin

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

···

2009/11/4 Martin DeMello <martindemello@gmail.com>:

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

taw's blog: Autovivification in Ruby

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme 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.

···

--
Posted via http://www.ruby-forum.com/\.

irb(main):001:0> a = Proc.new {|h,k| h[k] = Hash.new(&h.default_proc)}
=> #<Proc:0xb7d81730@(irb):1>
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.

···

On Wed, Nov 4, 2009 at 6:10 PM, Aldric Giacomoni <aldric@trevoke.net> wrote:

Robert Klemme 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.

Robert Klemme 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

···

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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@(irb):1>
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.

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Thu, Nov 5, 2009 at 8:25 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

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

Robert Klemme 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.

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

···

2009/11/5 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Thu, Nov 5, 2009 at 8:25 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:

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

Robert Klemme 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.

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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

···

2009/11/5 Jes�s Gabriel y Gal�n <jgabrielygalan@gmail.com>:

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.
--
Posted via http://www.ruby-forum.com/\.