Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.45 KB

hash.adoc

File metadata and controls

58 lines (43 loc) · 1.45 KB

Topic Name

Overview

Hash is similar to Array but it is a indexed collection of object references. However, while integer is used as an index in Array, Hash’s index can be objects of any types like String, Regular Expression and so on. When you store a value in a Hash, you are actually supplying two objects - the index (normally called the key) and the value. You can subsequently retrieve the value by using the Hash key(index).

Note
The values in a Hash can be objects of any type. Hash default value is nil when an attempt is made to access key that doesn’t exist.

Try yourself

  • Symbol as key

h1 = { :font_size=> 10, :font_family => "Arial" }
puts 'length => ', h1.length
puts h1[:font_size]
puts h1
  • String as key

h2 = {'dog'=> 'canine', 'cat' => 'feline', 'donkey' => 'asinine', 12 => 'dodecine'}
puts 'length => ', h2.length
puts h2['dog']
puts h2
puts h2[12]
  • Value update

h1 = { abc: "foo" } # {:abc=>"foo"}
h1[:abc]						# "foo"
h1[:abc] = "bar"		# "bar"
h1[:abc]						# "bar"
Warning
The key in the format {symbol: value} can’t be numeric. Eg. {1: 'one'} will not work but {1 ⇒ 'one'} will work.

Next Topic

Proceed to TODO

Previous Topic

Go back to Symbol

Table of Content