From: Joey Zhou Date: 2011-05-25T22:35:33+09:00 Subject: [ruby-core:36458] [Ruby 1.9 - Feature #4772] Hash#add_keys Issue #4772 has been updated by Joey Zhou. Yukihiro Matsumoto wrote: > I see no reason to add a method to generate keys only. Hash is a set of key-value pairs, right? Can you elaborate why you want this method? > > matz. Maybe the method name leads to misunderstanding. What I mean is that I give keys, the hash generate values automatically, not generating keys only. For example: hash = Hash.new {|h,k| k + k.succ} hash.add_keys("a","b","c") Now, the hash should be {"a"=>"ab","b"=>"bc","c"=>"cd"} In recent version, if I want to do the same thing, the code may be: hash = Hash.new {|h,k| k + k.succ} ["a","b","c"].each do |key| hash[key] = hash[key] # this kind of assignment is somewhat odd end hash = Hash.new {|h,k| h[k] = k + k.succ} ["a","b","c"].each do |key| hash[key] # making key appears means making it exists? odd too. end So I think a method "filling hash with given keys and auto-generated values" is OK. The name and parameter detail may be thought over. Joey ---------------------------------------- Feature #4772: Hash#add_keys http://redmine.ruby-lang.org/issues/4772 Author: Joey Zhou Status: Open Priority: Normal Assignee: Category: Target version: Hi, do you want to add a new method Hash#add_keys in a new version? hash = Hash.new {|h,k| k.to_s + "foo" } hash.add_keys("a","b","c") # the value is hash's default obj or proc value If there's a word list file, I want to make the words keys of a hash, maybe I can write: hash = {} hash.add_keys(*open("file").readlines.map(&:chomp)) -- http://redmine.ruby-lang.org