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 ?
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 ?
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:
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:
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.
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.
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.
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.
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