Project

General

Profile

Actions

Feature #4772

closed

Hash#add_keys

Added by yimutang (Joey Zhou) over 13 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
[ruby-core:36419]

Description

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))

Updated by matz (Yukihiro Matsumoto) over 13 years ago

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.

Updated by aprescott (Adam Prescott) over 13 years ago

On Tue, May 24, 2011 at 9:36 AM, Joey Zhou wrote:

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 default proc, I can't see why you'd need to create the keys
like this. If you did want to, can you not just use

keys.each { |k| hash[keys] }

?

For instance:

open("file").readlines.map(&:chomp).each { |k| hash[k] }

Updated by yimutang (Joey Zhou) over 13 years ago

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

Updated by yimutang (Joey Zhou) over 13 years ago

Adam Prescott wrote:

On Tue, May 24, 2011 at 9:36 AM, Joey Zhou wrote:

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 default proc, I can't see why you'd need to create the keys
like this. If you did want to, can you not just use

keys.each { |k| hash[keys] }

?

For instance:

open("file").readlines.map(&:chomp).each { |k| hash[k] }

This doesn't create the actual key/value pair, the way seems not explicit, I'm afraid.

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago

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

Actually, as already noticed here, this could be just:

['a', 'b', 'c'].each {|k| hash[k]} # You don't need to assign to it.

But I like your suggestion anyway...

Updated by mame (Yusuke Endoh) over 13 years ago

Hello,

2011/5/25 Rodrigo Rosenfeld Rosas :

Actually, as already noticed here, this could be just:

['a', 'b', 'c'].each {|k| hash[k]} # You don't need to assign to it.

The assignment is needed.

hash = Hash.new {|h,k| k + k.succ}
["a","b","c"].each {|k| hash[k] }

p hash #=> {} <- LOOK!

This feature might be useful to use a hash as a set...
I'm not sure though.

--
Yusuke Endoh

Updated by yimutang (Joey Zhou) over 13 years ago

Rodrigo Rosenfeld Rosas wrote:

Actually, as already noticed here, this could be just:

['a', 'b', 'c'].each {|k| hash[k]} # You don't need to assign to it.

Well, it is very obscure here, easy to be confused.

  1. hash = Hash.new {|h,k| k.succ }

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

  3. and 2) are different, 2) will actually create any key/value pair the hash ever seen a key, but 1) will not, it needs an assignment.

Joey

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 13 years ago

Em 25-05-2011 10:54, Yusuke ENDOH escreveu:

Hello,

2011/5/25 Rodrigo Rosenfeld Rosas:

Actually, as already noticed here, this could be just:

['a', 'b', 'c'].each {|k| hash[k]} # You don't need to assign to it.

The assignment is needed.

hash = Hash.new {|h,k| k + k.succ}
["a","b","c"].each {|k| hash[k] }

p hash #=> {}<- LOOK!

This feature might be useful to use a hash as a set...
I'm not sure though.

Yes, I didn't read the documentation and believed on Adam statement,
sorry :)

I think that "add_keys" makes the code more readable than the current
alternative.

Or maybe "add_keys_with_default_value" :)

Updated by aprescott (Adam Prescott) over 13 years ago

On Wed, May 25, 2011 at 3:20 PM, Joey Zhou wrote:

  1. hash = Hash.new {|h,k| k.succ }

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

  3. and 2) are different, 2) will actually create any key/value pair the
    hash ever seen a key, but 1) will not, it needs an assignment.

Ah I see, I missed the lack of assignment in the block to Hash.new. I
suppose potentially there's some confusion that might come up when using
Hash#add_keys and Hash.new given that one block is being used for both
purposes?

Updated by mame (Yusuke Endoh) over 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) about 12 years ago

  • Target version set to 2.6

Updated by matz (Yukihiro Matsumoto) about 12 years ago

  • Status changed from Assigned to Rejected

As far as I understand, the OP intention was to generate key/default-value pair explicitly.
But I don't see any real world use case. The value will be generated anyway.
If he wants to cache the value, the name #add_keys does not describe the intention.

So I mark this "rejected". If OP (or somebody else) come up with real use case, please re-open.

Matz.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0