Special Hash Constructors

Hi there,

we frequently do

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

so we can later do

ha[any_key] << anything

Since the idiom is so common, what does everybody think of putting it
into the standard library:

def Hash.with_arrays
new {|h,k| h[k] = []}
end

While we’re at it, we might as well add

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

Kind regards

robert

On Fri, Feb 6, 2009 at 9:09 AM, Robert K.
[email protected] wrote:

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

Kind regards

   robert

Rather than making class methods on Hash to create special hashes like
this, why not make subclasses, so you can attach specialized instance
methods that make sense only for these kinds of hashes. For instance,
for the Hash-of-Hashes form, you might want a method that “rotates”
the Hash into a new Hash-of-Hashes like this:

def rotate
res = HashOfHashes.new
each { |ok, ov| ov.each { |ik, iv| res[ik][ok] = iv } }
res
end

Wow I like that. This is what I end up doing:

ha = {}
(ha[any_key] ||= []) << anything

J

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

Since the idiom is so common, what does everybody think of putting it
into the standard library:

Why not

def Hash.with(default=nil)
new { | h, k | h[k] = default.dup }
end

While we’re at it, we might as well add

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

I’m too tired for that. Does that create Hash with a default proc that
is the same as the one you specify right there?

mfg, simon … l

On Sat, Feb 7, 2009 at 5:34 AM, Simon K. [email protected] wrote:

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

I’m too tired for that. Does that create Hash with a default proc that
is the same as the one you specify right there?

Yes, it achieves having infinitely nested hashes:

irb(main):001:0> def Hash.with_hashes
irb(main):002:1> new {|h,k| h[k] = Hash.new(&h.default_proc)}
irb(main):003:1> end
=> nil
irb(main):004:0> h = Hash.with_hashes
=> {}
irb(main):005:0> h[1][2][3] = true
=> true
irb(main):006:0> h
=> {1=>{2=>{3=>true}}}

I agree that it would be nice to have them in the standard library,
but maybe Hash.of_hashes and Hash.of_arrays would be better names?

Jesus.

def Hash.with(default=nil)
new { | h, k | h[k] = default.dup }
end

Not so good:

h = Hash.with(0)
h[1]
TypeError: can’t dup Fixnum

I personally would appreciate a special constructor for Arrays.

On 07.02.2009 11:09, Jesús Gabriel y Galán wrote:

On Sat, Feb 7, 2009 at 5:34 AM, Simon K. [email protected] wrote:

I agree that it would be nice to have them in the standard library,
but maybe Hash.of_hashes and Hash.of_arrays would be better names?

Yes, probably. I’m not religious about the names. My point was simply
that this is so often used that we might want to have it in the standard
library.

Cheers

robert

On Feb 6, 12:09 pm, Robert K. [email protected] wrote:

Since the idiom is so common, what does everybody think of putting it
end
I’ve been using the name #autonew for this last one.

Also @Tom L.

def Hash.with(default=nil)
begin
new { | h, k | h[k] = default.dup }
rescue TypeError
new(default) # same as { | h, k | h[k] = default }
end
end

T.

def Hash.with(default=nil)
begin

I think we have to insert a check for “dupability” here:

    default.dup
  new { | h, k | h[k] = default.dup }

This method isn’t capable of emulating the with_hashes method proposed
by Robert K though, is it?

On Feb 7, 8:18 am, Robert K. [email protected] wrote:

Where’s the point of the rescue clause? The error will show up much
later, i.e. after the method has returned.

Why would the error show later? It is just so one could pass a
immutable, like 0 to #with without raising an error b/c it doesn’t
respond to #dup.

Also, as Tom pointed out, this is not the same as what I proposed - not
even similar.

It’s not?

Hash.with([])

is not the same as

Hash.with_arrays

?

On 07.02.2009 11:26, Trans wrote:

ha[any_key] << anything
def Hash.with_hashes
rescue TypeError
new(default) # same as { | h, k | h[k] = default }
end
end

Where’s the point of the rescue clause? The error will show up much
later, i.e. after the method has returned.

Also, as Tom pointed out, this is not the same as what I proposed - not
even similar.

Regards

robert

2009/2/7 Tom L. [email protected]:

Not so good:
TypeError: can’t dup Fixnum

I often use

class Hash
def self.new_with
new { |h, k| h[k] = yield }
end
end

Hash.new_with { [] }

which works with Arrays and Fixnums, though it isn’t needed for the
latter.

Regards,
Pit

by Robert K though, is it?
I think that’s special enough to have its own method.

mfg, simon … l

On 07.02.2009 16:32, Trans wrote:

On Feb 7, 8:18 am, Robert K. [email protected] wrote:

Where’s the point of the rescue clause? The error will show up much
later, i.e. after the method has returned.

Why would the error show later? It is just so one could pass a
immutable, like 0 to #with without raising an error b/c it doesn’t
respond to #dup.

The code block is not executed when you invoke Hash.with - hence there
can’t be an error. When you need it the rescue block is not there as
the block is called from a different context.

?

It’s closer than I initially thought. But: I wanted these two special
methods and not yet another generic method. For that, we have the
original Hash.new already. Your version still needs an argument. Since
I see that we are using an Array most of the time anyway that can be
coded into the method. No need for another generic approach IMHO.

Cheers

robert

It’s closer than I initially thought. But: I wanted these two special
methods and not yet another generic method. For that, we have the
original Hash.new already. Your version still needs an argument. Since
I see that we are using an Array most of the time anyway that can be
coded into the method. No need for another generic approach IMHO.

The difference between the existing and the new generic is the
assignment. I see the block form of new most often because some mutable
object should be assigned on read.

mfg, simon … l