ruby - What is meant: "Hash.new takes a default value for the hash, which is the value of the hash for a nonexistent key" -


i'm going through ruby on rails tutorial michael hartl

not understanding meaning of statement found in section 4.4.1:

hashes, in contrast, different. while array constructor array.new takes initial value array, hash.new takes default value hash, value of hash nonexistent key:

could explain meant this? don't understand author trying @ regarding how hashes differ arrays in context of section of book

you can try out code in irb or rails console find out mean.

array.new # => []  array.new(7) # => [nil, nil, nil, nil, nil, nil, nil]  h1 = hash.new h1['abc'] # => nil  h2 = hash.new(7) h2['abc'] # => 7 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -