Ruby hash default value behavior
The other answers seem to indicate that the difference in behavior is due to Integer
s being immutable and Array
s being mutable. But that is misleading. The difference is not that the creator of Ruby decided to make one immutable and the other mutable. The difference is that you, the programmer decided to mutate one but not the other.
The question is not whether Array
s are mutable, the question is whether you mutate it.
You can get both the behaviors you see above, just by using Array
s. Observe:
One default Array
with mutation
hsh = Hash.new([])
hsh[:one] << 'one'
hsh[:two] << 'two'
hsh[:nonexistent]
# => ['one', 'two']
# Because we mutated the default value, nonexistent keys return the changed value
hsh
# => {}
# But we never mutated the hash itself, therefore it is still empty!
One default Array
without mutation
hsh = Hash.new([])
hsh[:one] += ['one']
hsh[:two] += ['two']
# This is syntactic sugar for hsh[:two] = hsh[:two] + ['two']
hsh[:nonexistant]
# => []
# We didn't mutate the default value, it is still an empty array
hsh
# => { :one => ['one'], :two => ['two'] }
# This time, we *did* mutate the hash.
A new, different Array
every time with mutation
hsh = Hash.new { [] }
# This time, instead of a default *value*, we use a default *block*
hsh[:one] << 'one'
hsh[:two] << 'two'
hsh[:nonexistent]
# => []
# We *did* mutate the default value, but it was a fresh one every time.
hsh
# => {}
# But we never mutated the hash itself, therefore it is still empty!
hsh = Hash.new {|hsh, key| hsh[key] = [] }
# This time, instead of a default *value*, we use a default *block*
# And the block not only *returns* the default value, it also *assigns* it
hsh[:one] << 'one'
hsh[:two] << 'two'
hsh[:nonexistent]
# => []
# We *did* mutate the default value, but it was a fresh one every time.
hsh
# => { :one => ['one'], :two => ['two'], :nonexistent => [] }
Working with Hashes that have a default value
0 will be the fallback if you try to access a key in the hash that doesn't exist
For example:
count = Hash.new
-> count['key'] => nil
vs
count = Hash.new(0)
-> count['key'] => 0
Default value is the same object in hash data type in ruby
hash = Hash.new([])
This statement creates an empty hash that has a default value of an empty array. If hash
does not have a key k
, hash[k]
returns the default value, []
. This is important: simply returning the default value does not modify the hash.
When you write:
hash[:one] << "uno" #=> ["uno"]
before hash
has a key :one
, hash[:one]
is replaced by the default value, so we have:
[] << "uno" #=> ["uno"]
which explains why hash
is not changed:
hash #=> {}
Now write:
hash[:two] << "dos" #=> ["uno", "dos"]
hash #=> {}
To see why we get this result, let's re-write the above as follows:
hash = Hash.new([]) #=> {}
hash.default #=> []
hash.default.object_id #=> 70209931469620
a = (hash[:one] << "uno") #=> ["uno"]
a.object_id #=> 70209931469620
hash.default #=> ["uno"]
b = (hash[:two] << "dos") #=> ["uno", "dos"]
b.object_id #=> 70209931469620
hash.default #=> ["uno", "dos"]
So you see that the default array whose object_id
is 70209931469620
is the single array to which "uno" and "dos" are appended.
If we had instead written:
hash[:one] = hash[:one] << "uno"
#=> hash[:one] = [] << "uno" => ["uno"]
hash #=> { :one=>"uno" }
we get what we were hoping for, but not so fast:
hash[:two] = hash[:two] << "dos"
#=> ["uno", "dos"]
hash
#=> {:one=>["uno", "dos"], :two=>["uno", "dos"]}
which is still not what we want because both keys have values that are the same array.
hash = Hash.new {|hash, key| hash[key] = [] }
This statements causes the block to be executed when hash
does not have a key key
. This does change the hash, by adding a key value pair1.
So now:
hash[:one] << "uno"
causes the block:
{ |h,k| h[k] = [] }
to make the assignment:
hash[:one] = []
after which:
hash[:one] << "uno"
appends "uno"
to an empty array that is the value for the key :one
, which we can verify:
hash #=> { :one=>"uno" }
This has the same effect as writing:
hash[:one] = (hash[:one] || []) << "uno"
(the expanded version of (hash[:one] ||= []) << "uno"
) when there is no default value.
Similarly,
hash[:two] << "dos" #=> ["dos"]
hash #=> {:one=>["uno"], :two=>["dos"]}
which is usually the result we want.
1 The block need not change the hash, however. The block can contain any code, including, for example, { puts "Have a nice day" }
.
Ruby: How to set a default value for nil keys on a hash created via hash literal notation?
The {}
syntax to generate a new hash in Ruby doesn't support setting a default. But you can use Hash#default=
to set an default too:
hash = {}
hash.default = "default value"
hash[:foo]
#=> "default value"
Setting ruby hash .default to a list
Hash.default
is used to set the default value returned when you query a key that doesn't exist. An entry in the collection is not created for you, just because queried it.
Also, the value you set default
to is an instance of an object (an Array in your case), so when this is returned, it can be manipulated.
a = {}
a.default = [] # set default to a new empty Array
a[8] << 9 # a[8] doesn't exist, so the Array instance is returned, and 9 appended to it
a.default # => [9]
a[9] # a[9] doesn't exist, so default is returned
rails nested hash with default values
The doc for Hash::new explains the three ways of initializing a hash and, in your case, you are using an object in the Hash constructor:
If obj is specified, this single object will be used for all default values.
If you want that each missing key creates it's own object, create the hash with a block, like this:
h = Hash.new { |h,k| h[k] = { count: 0, rating: 0 } }
Then:
2.6.3 :012 > h
=> {}
2.6.3 :013 > h['a'][:count] = 5
=> 5
2.6.3 :015 > h
=> {"a"=>{:count=>5, :rating=>0}}
Hash default value is a hash with same default value
Sort of mind bending, but you can pass the hash's default_proc
to the inner hash:
hash = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
hash[:foo] #=> {}
hash[:foo][:bar] #=> {}
hash[:foo][:bar][:baz] #=> {}
hash #=> {:foo=>{:bar=>{:baz=>{}}}}
Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])
First, note that this behavior applies to any default value that is subsequently mutated (e.g. hashes and strings), not just arrays. It also applies similarly to the populated elements in Array.new(3, [])
.
TL;DR: Use Hash.new { |h, k| h[k] = [] }
if you want the most idiomatic solution and don’t care why.
What doesn’t work
Why Hash.new([])
doesn’t work
Let’s look more in-depth at why Hash.new([])
doesn’t work:
h = Hash.new([])
h[0] << 'a' #=> ["a"]
h[1] << 'b' #=> ["a", "b"]
h[1] #=> ["a", "b"]
h[0].object_id == h[1].object_id #=> true
h #=> {}
We can see that our default object is being reused and mutated (this is because it is passed as the one and only default value, the hash has no way of getting a fresh, new default value), but why are there no keys or values in the array, despite h[1]
still giving us a value? Here’s a hint:
h[42] #=> ["a", "b"]
The array returned by each []
call is just the default value, which we’ve been mutating all this time so now contains our new values. Since <<
doesn’t assign to the hash (there can never be assignment in Ruby without an =
present†), we’ve never put anything into our actual hash. Instead we have to use <<=
(which is to <<
as +=
is to +
):
h[2] <<= 'c' #=> ["a", "b", "c"]
h #=> {2=>["a", "b", "c"]}
This is the same as:
h[2] = (h[2] << 'c')
Why Hash.new { [] }
doesn’t work
Using Hash.new { [] }
solves the problem of reusing and mutating the original default value (as the block given is called each time, returning a new array), but not the assignment problem:
h = Hash.new { [] }
h[0] << 'a' #=> ["a"]
h[1] <<= 'b' #=> ["b"]
h #=> {1=>["b"]}
What does work
The assignment way
If we remember to always use <<=
, then Hash.new { [] }
is a viable solution, but it’s a bit odd and non-idiomatic (I’ve never seen <<=
used in the wild). It’s also prone to subtle bugs if <<
is inadvertently used.
The mutable way
The documentation for Hash.new
states (emphasis my own):
If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.
So we must store the default value in the hash from within the block if we wish to use <<
instead of <<=
:
h = Hash.new { |h, k| h[k] = [] }
h[0] << 'a' #=> ["a"]
h[1] << 'b' #=> ["b"]
h #=> {0=>["a"], 1=>["b"]}
This effectively moves the assignment from our individual calls (which would use <<=
) to the block passed to Hash.new
, removing the burden of unexpected behavior when using <<
.
Note that there is one functional difference between this method and the others: this way assigns the default value upon reading (as the assignment always happens inside the block). For example:
h1 = Hash.new { |h, k| h[k] = [] }
h1[:x]
h1 #=> {:x=>[]}
h2 = Hash.new { [] }
h2[:x]
h2 #=> {}
The immutable way
You may be wondering why Hash.new([])
doesn’t work while Hash.new(0)
works just fine. The key is that Numerics in Ruby are immutable, so we naturally never end up mutating them in-place. If we treated our default value as immutable, we could use Hash.new([])
just fine too:
h = Hash.new([].freeze)
h[0] += ['a'] #=> ["a"]
h[1] += ['b'] #=> ["b"]
h[2] #=> []
h #=> {0=>["a"], 1=>["b"]}
However, note that ([].freeze + [].freeze).frozen? == false
. So, if you want to ensure that the immutability is preserved throughout, then you must take care to re-freeze the new object.
Conclusion
Of all the ways, I personally prefer “the immutable way”—immutability generally makes reasoning about things much simpler. It is, after all, the only method that has no possibility of hidden or subtle unexpected behavior. However, the most common and idiomatic way is “the mutable way”.
As a final aside, this behavior of Hash default values is noted in Ruby Koans.
† This isn’t strictly true, methods like instance_variable_set
bypass this, but they must exist for metaprogramming since the l-value in =
cannot be dynamic.
Hash default-value creates all of the same instance
If you give Hash.new
an object (like another Hash.new
) this very object is the default value. It is shared across different keys.
default = []
hash = Hash.new(default)
hash[:one] << 1
# now default is [1] !
You want to use Hash.new with a block, so that something new happens each time a key was not found.
Like
h = Hash.new { |hash,new_key| hash[new_key] = {} }
There is great explanation about that e.g. in Ruby hash default value behavior . Your question is kind of a duplicate.
Also, as we figured out in the comments, you probably wanted to compare the object_id
and not the hash
value.
first_hash = {}
second_hash = {}
# .hash same, but different objects!
puts "initial, hash:"
puts first_hash.hash == second_hash.hash ? " - same" : " - differs"
puts "initial, object_id"
puts first_hash.object_id == second_hash.object_id ? " - same" : " - differs"
puts
# Change the world
# .hash different, and still different objects.
first_hash[:for] = "better"
puts "better world now, hash:"
puts first_hash.hash == second_hash.hash ? " - same" : " - differs"
puts "better world now, object_id"
puts first_hash.object_id == second_hash.object_id ? " - same" : " - differs"
Related Topics
Actionview::Template::Error (Incompatible Character Encodings: Utf-8 and Ascii-8Bit)
Using Ruby Convert Numbers to Words
Set Utf-8 as Default for Ruby 1.9.3
Rails - Whenever Gem - Dynamic Values
Project Euler 1:Find the Sum of All the Multiples of 3 or 5 Below 1000
How to Say Something Happened "X Minutes Ago" or "X Hours Ago" or "X Days Ago" in Ruby
How to Set Http_Referer When Testing in Rails
Errno::Econnrefused: Connection Refused - Connect(2) for Action Mailer
Differencebetween 'Range#Include' and 'Range#Cover'
Is There a Built-In Binary-Search in Ruby
Ruby 'Encode': "\Xc3" from Ascii-8Bit to Utf-8 (Encoding::Undefinedconversionerror)
How to Convert a Ruby Hash to Xml
Typing 'Rails Console' Doesn't Start
Why Can't Singleton Methods Be Defined on Symbols or Fixnums
Looping Through an Array with Step