From: Conrad Schneiker Date: 2001-02-16T10:04:04+09:00 Subject: [ruby-talk:10938] Re: Making Hash from two lists Kenichi Komiya # 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? Well, I think it should be in 'standard' Ruby. (I'm never sure what people mean by 'core', since some people think of this as what is built into Ruby, versus Ruby code in a Ruby library.) My preference is for all these sorts of generalized data transformation tools to be in Ruby out of the box. Even though it's easy to reinvent your own wheels, axles, transmissions, crankshafts, and motors in Ruby, I think it's still a good idea to give people a pretty extensive, pre-coordinated, and well-coordinated system to start off with. # Q2: Do you think the name is confusing? If so what is good # name for this method? I think it maybe wants to be _map or map_. I don't know what should be, however. # Q3: I belive there was a discussion to create a snipet # repository, which archives useful but small code examples. # Is this facility already online? Well, there is (http://www.pragprog.com:8080/ruby?RubyOnlineCookbook). Unfortunately there don't seem to be many handy links around to point people here (or to the more general area around it). Anyone want to change that? Conrad Schneiker (This note is unofficial and subject to improvement without notice.)