Hi,

In [ruby-list:27832], I posted a code snipet that let you create
a Hash from lists of keys and values.

  hash = Hash.map(%w(Matz Larry), %(Ruby, Perl))

  p hash    # => {"Larry"=>"Perl", "Matz"=>"Ruby"}

The signature of this method looks like:

  Hash.map(keys, vals, def_val=nil)

Some details.

  * Keys and vals can be any Enumerable object.

  * If keys is longer than vals, def_val is assigned to these extra
    keys.

  * If vals is longer than keys, these extra valuess are ignored.
    (The vals enumerable is not iterated beyond length of keys).

matz's comments on this were:

  * The idea looks OK.

  * def_val may be an over design.

  * The name Hash.map may be confusing concidering existing
    Hash#map, which does wildly different thing.

So, if people like this functionality and a good name is
proposed, this may be included in some future version of
interpretter.

Some questions:

  Q1: Do you think this is worth including to core Ruby?

  Q2: Do you think the name is confusing?  If so what is good
      name for this method?

  Q3: I belive there was a discussion to create a snipet
      repository, which archives useful but small code examples.
      Is this facility already online?

My original intent of the post was just sharing my idea with
others.  So I, personally, am more interested in the code snipet
repository thing than wether or not this will go into core Ruby.


== Hash.map ==

def Hash.map(keys, vals, def_val=nil)
  map_array = []
  i = j = 0
  keys.each_with_index do |k, i|
    map_array[i * 2] = k
    map_array[i * 2 + 1] = def_val
  end
  vals.each_with_index do |v, j| 
    map_array[j * 2 + 1] = v
    break if j == i
  end
  self[*map_array]
end

p Hash.map(%w(foo bar zot), %w(1 2 3))
p Hash.map(%w(foo), %w(1 2 3))
p Hash.map(%w(foo bar zot), %w(1), false)

== output ==
{"zot"=>"3", "bar"=>"2", "foo"=>"1"}
{"foo"=>"1"}
{"zot"=>false, "bar"=>false, "foo"=>"1"}


Regards,
Kenichi Komiya