A Hash is a dictionary-like collection of unique
keys and their values. Also called associative arrays, they are similar to
Arrays, but where an Array uses integers as its
index, a Hash allows you to use any object type.
Hashes enumerate their values in the order that the corresponding keys were inserted.
Creating Hash: There are several ways to create a Hash. They are:
1.Implicit way: You can create a hash implicitly like:
_hash = { 'key1' => 'value1' , 'key2' => 'value2' }
Hashes allow an alternate syntax form when your keys are always symbols. Instead of
_hash = { :key1 => 'value1' , :key2 => 'value2' }
You could write it as:
_hash = { key1: 'value1' , key2: 'value2' }
2. Use ::new method to create Hash: You can also create hash by ::new method.
_hash = Hash.new
Default value of a Hash : Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used. You can set the default value by sending it as an argument to ::new:
_hash = Hash.new(10)
If you access a key which is not exist in the hash then default value will be return.In the above example
_hash[:key1] // this will return 10 because key1 is not exists in hash, hence it will give result 10.
you can also set the default value as an array or another hash. Like:
_hash = Hash.new([]) // setting default value as a blank array.
_hash[:a] // will return a blank array.
_hash[:a] << 20 // this will insert a value in default value that is an array.if you just print _hash then it is a blank hash no key in this hash but its default value has been changes from a blank array to a array that contains a value 20.
now if you print _hash[:b] // will give result [20] because default value is changed.
Hashes enumerate their values in the order that the corresponding keys were inserted.
Creating Hash: There are several ways to create a Hash. They are:
1.Implicit way: You can create a hash implicitly like:
_hash = { 'key1' => 'value1' , 'key2' => 'value2' }
Hashes allow an alternate syntax form when your keys are always symbols. Instead of
_hash = { :key1 => 'value1' , :key2 => 'value2' }
You could write it as:
_hash = { key1: 'value1' , key2: 'value2' }
2. Use ::new method to create Hash: You can also create hash by ::new method.
_hash = Hash.new
Default value of a Hash : Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used. You can set the default value by sending it as an argument to ::new:
_hash = Hash.new(10)
If you access a key which is not exist in the hash then default value will be return.In the above example
_hash[:key1] // this will return 10 because key1 is not exists in hash, hence it will give result 10.
you can also set the default value as an array or another hash. Like:
_hash = Hash.new([]) // setting default value as a blank array.
_hash[:a] // will return a blank array.
_hash[:a] << 20 // this will insert a value in default value that is an array.if you just print _hash then it is a blank hash no key in this hash but its default value has been changes from a blank array to a array that contains a value 20.
now if you print _hash[:b] // will give result [20] because default value is changed.
No comments:
Post a Comment