Project

General

Profile

This project is closed and read-only.

Actions

Bug #2004

closed

Setting Hash default to an array hides the hash keys

Bug #2004: Setting Hash default to an array hides the hash keys

Added by totoro (Alan Stebbens) about 17 years ago. Updated over 15 years ago.

Status:
Rejected
Assignee:
-
ruby -v:
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9.5.0]
[ruby-core:25157]

Description

=begin
Setting the default value of a hash to an array causes the keys and values methods results to be empty.

It may be counter-intuitive and not actually useful, but the assignment of the default value to an array causes all values to be same array object.

So, the little sums and subtractions end up applying to the same array object, no matter what the hash key is.

However, even though the hash keys and values appear to be empty, they are clearly still there.

% cat bug1.rb
#!/usr/bin/env ruby

h = Hash.new [0, 0]
d = ['a', 'b', 'c']
puts h

d.each_with_index{|k,x|
h[k][0] += x
h[k][1] -= x
}

puts "h.inspect = #{h.inspect}"
puts "h.keys = #{h.keys.inspect}"
puts "h.values = #{h.values.inspect}"

d.each{|k| puts "#{k} = #{h[k].inspect}"}


$ ruby bug1.rb

h.inspect = {}
h.keys = []
h.values = []
a = [3, -3]
b = [3, -3]
c = [3, -3]
=end


Files

bug1.rb (277 Bytes) bug1.rb bug1.rb totoro (Alan Stebbens), 08/27/2009 01:26 PM
good1.rb (302 Bytes) good1.rb good1.rb totoro (Alan Stebbens), 08/27/2009 01:35 PM

Related issues 1 (0 open — 1 closed)

Has duplicate Backport187 - Bug #2006: Setting Hash default to an array hides the hash keysRejectedActions

Updated by totoro (Alan Stebbens) about 17 years ago Actions #1

=begin
This is the same code, except without an array default value (using code to initialize a default for each key):

$ cat good1.rb
#!/usr/bin/env ruby

h = Hash.new
d = ['a', 'b', 'c']
puts h

d.each_with_index{|k,x|
h[k] = [0,0] unless h.key? k
h[k][0] += x
h[k][1] -= x
}

puts "h.inspect = #{h.inspect}"
puts "h.keys = #{h.keys.inspect}"
puts "h.values = #{h.values.inspect}"

d.each{|k| puts "#{k} = #{h[k].inspect}"}


ruby good1.rb

h.inspect = {"a"=>[0, 0], "b"=>[1, -1], "c"=>[2, -2]}
h.keys = ["a", "b", "c"]
h.values = [[0, 0], [1, -1], [2, -2]]
a = [0, 0]
b = [1, -1]
c = [2, -2]

=end

Updated by nobu (Nobuyoshi Nakada) about 17 years ago Actions #2

=begin
Completely expected behavior.
=end

Updated by totoro (Alan Stebbens) about 17 years ago Actions #3

=begin
It is expected that having a default value of an array should cause h.keys and h.values to be empty, even when there are keys and values?

Why?
=end

Updated by nobu (Nobuyoshi Nakada) about 17 years ago Actions #4

=begin
Hi,

At Thu, 27 Aug 2009 13:59:17 +0900,
Alan Stebbens wrote in [ruby-core:25162]:

It is expected that having a default value of an array
should cause h.keys and h.values to be empty, even when there
are keys and values?

You don't assign any key at all. The default value is not
automatically assigned. You may want to use default_proc
instead.

--
Nobu Nakada

=end

Updated by marcandre (Marc-Andre Lafortune) about 17 years ago Actions #5

  • Status changed from Open to Rejected

=begin
Closing since this it the official behavior, as explained in detail in Hash.new, #default and #default=
=end

Actions

Also available in: PDF Atom