Issue #6641 has been updated by Eregon (Benoit Daloze).


trans (Thomas Sawyer) wrote:
> It is not uncommon to need a Hash following the pattern:
> 
>   Hash.new{ |h,k| h[k]=[] }
>   
> This is common enough that it would very nice if we could have a more concise form, e.g.
> 
>   Hash.auto{ [] }

I agree, this is a pattern I see quite often and the `Hash.new { |h,k| h[k] = ... }` form is not the most natural or expressive in my opinion.
But at the same time, this is clearly a specialization of Hash.new with a block, which loses some flexibility (you can not use the key for example). I'm curious what others think of it.
----------------------------------------
Feature #6641: Hash.auto constructor
https://bugs.ruby-lang.org/issues/6641#change-27407

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


=begin
It is not uncommon to need a Hash following the pattern:

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

Another common example:

  Hash.new{ |h,k| h[k]=[] }
  
This is common enough that it would very nice if we could have a more concise form, e.g.

  Hash.auto{ {} }

Or for the second example:

  Hash.auto{ [] }

Pure Ruby implementation is pretty simple:

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

I think it would be nice to have in Core.

This is secondary, but it just occurred to me. Could there even be a literal notation for the above? Something like:

  {}([])

?
=end



-- 
http://bugs.ruby-lang.org/