Project

General

Profile

Actions

Feature #6641

open

Hash.auto constructor

Added by trans (Thomas Sawyer) almost 12 years ago. Updated over 6 years ago.

Status:
Open
Target version:
-
[ruby-core:45822]

Description

=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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0