access any key in a hash that has a default value : Hash.new « Hash « Ruby






access any key in a hash that has a default value


if the key or value doesn't exist, accessing the hash will return the default value:

months = Hash.new( "month" )

months[0]

# or:

months[72]

# or:

months[234]

 








Related examples in the same category

1.Creating Hashes
2.You can test to see if a hash is empty with empty?
3.Test how big it is with length or size
4.use new to create a hash with a default value which is otherwise just nil
5.Hash with =>
6.Hash.new with block logic
7.This lazily initialized hash maps integers to their factorials