Forum: Ruby Special Hash Constructors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert K. (Guest)
on 2009-02-06 19:10
(Received via mailing list)
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
Christopher D. (Guest)
on 2009-02-06 19:45
(Received via mailing list)
On Fri, Feb 6, 2009 at 9:09 AM, Robert K.
<removed_email_address@domain.invalid> 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
Julian L. (Guest)
on 2009-02-07 02:39
(Received via mailing list)
Wow I like that. This is what I end up doing:

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

J
Simon K. (Guest)
on 2009-02-07 06:35
(Received via mailing list)
* Robert K. <removed_email_address@domain.invalid> (18:05) schrieb:

> 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
Tom L. (Guest)
on 2009-02-07 08:17
(Received via mailing list)
> 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.
Jesús Gabriel y Galán (Guest)
on 2009-02-07 12:10
(Received via mailing list)
On Sat, Feb 7, 2009 at 5:34 AM, Simon K. <removed_email_address@domain.invalid> 
wrote:
> * Robert K. <removed_email_address@domain.invalid> (18:05) schrieb:

>> 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.
Robert K. (Guest)
on 2009-02-07 12:25
(Received via mailing list)
On 07.02.2009 11:09, Jesús Gabriel y Galán wrote:
> On Sat, Feb 7, 2009 at 5:34 AM, Simon K. <removed_email_address@domain.invalid> 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
Thomas S. (Guest)
on 2009-02-07 12:27
(Received via mailing list)
On Feb 6, 12:09 pm, Robert K. <removed_email_address@domain.invalid> 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.
Tom L. (Guest)
on 2009-02-07 12:48
(Received via mailing list)
>   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?
Robert K. (Guest)
on 2009-02-07 15:20
(Received via mailing list)
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
Thomas S. (Guest)
on 2009-02-07 17:34
(Received via mailing list)
On Feb 7, 8:18 am, Robert K. <removed_email_address@domain.invalid> 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

?
Pit C. (Guest)
on 2009-02-07 17:38
(Received via mailing list)
2009/2/7 Tom L. <removed_email_address@domain.invalid>:
> 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
Robert K. (Guest)
on 2009-02-07 18:40
(Received via mailing list)
On 07.02.2009 16:32, Trans wrote:
>
> On Feb 7, 8:18 am, Robert K. <removed_email_address@domain.invalid> 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
Simon K. (Guest)
on 2009-02-07 20:05
(Received via mailing list)
* Tom L. <removed_email_address@domain.invalid> (11:44) schrieb:

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

mfg,                    simon .... l
Simon K. (Guest)
on 2009-02-07 20:35
(Received via mailing list)
* Robert K. <removed_email_address@domain.invalid> (17:35) schrieb:

> 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
This topic is locked and can not be replied to.