Ruby Hash

Hash in ruby programming language is combination of key and value pair. Key and value can be any object, symbol or value. It is also called associative arrays, dictionaries or maps.

hash = #Define empty hash

In this example created an empty hash. We can add new element in this empty hash by using of key and its associate values. That's are similar to update element of array but in this case key is an user define. let seen an example to add element of this empty hash.

hash_record = #Define empty hash

#Add hash element
# :first is key and 100 is value
hash_record[:first]  = 100 
hash_record[:second] = 200
hash_record[:third]  = 300
hash_record[:fourth] = 50

print hash_record #display hash elements
Key and value pair in hash
{:first=>100, :second=>200, :third=>300, :fourth=>50}

If you can seen this output you are understand what is actual arrangement of hash element. Generally key are represented as symbols of hash and key reference will be use hash element. Observe that in above program output is showing on inside curly braces. In same way we can initialize hash element. See this example

hash_data = {:keyOne => 100, :keyTwo => "Code" ,:keyThree =>2050.50 }
puts hash_data
Hash data representation in ruby
{:keyOne=>100, :keyTwo=>"Code", :keyThree=>2050.5}

Hash element are access by particular key. This key can be form of a any values like number, String etc. Element of hash are used by key reference. That is similar to access array element but in this case not use index array here used particular key. let's see others ways to define a hash in ruby programming.

Example 1

#Convert array to hash
data=Hash[1 => "One", 5 => "Five", 9=>"Nine"]
puts "#{data[1]}"
puts "#{data[5]}"
puts "#{data[9]}"

Example 2

hash_data = {name: 'smith', language: 'English', surname: 'wood'}  

puts hash_data
{:name=>"smith", :language=>"English", :surname=>"wood"}

Basic operation and methods

Set default access key values

When we are try to access invalid key of hash element in this situation we are get nil value. In this case we can set default value in this ways. #default value
puts data[1]  #100
puts data[:salary] #100

puts data #{}
#This are prints default value of  hash
puts data.default #100

In other ways

def check_default(my_hash)
  if my_hash.default==nil
    puts "Default Value : nil"
    puts "Default Value : #{my_hash.default}"
  puts "Hase Elements"
  puts my_hash
test_data = {1=>"Ruby"}

check_default test_data

#after set default value

test_data.default = 11 #default 11

check_default test_data
Default Value : nil
Hase Elements
Default Value : 11
Hase Elements

Get hash keys and values

data = {1=>"In",2=>"Out",true=>"Code"}
#return array of hash keys
puts "#{data.keys}" #[1, 2, true]

#return array of hash keys
puts "#{data.values}" #["In", "Out", "Code"]

Iterating hash elements

hash_data = {'A'=>"Algorithm",'B'=>"Best",'C'=>"Complier",'D'=>"Debug"}

hash_data.each{ |key,value|  puts "#{key} is #{value}" }
A is Algorithm
B is Best
C is Complier
D is Debug
hash_data = {'A'=>"Algorithm",'B'=>"Best",'C'=>"Complier",'D'=>"Debug"}

hash_data.each_key{ |key|  puts key }
hash_data = {'A'=>"Algorithm",'B'=>"Best",'C'=>"Complier",'D'=>"Debug"}

hash_data.each_value{ |val|  puts val }

Check if key exists in hash

hash_data = {'A'=>"Algorithm",'B'=>"Best",'C'=>"Complier",'D'=>"Debug"}
puts hash_data.key?("A") #true
puts hash_data.key?("Z") #false

Delete hash element by key

#Hash elements
data = {1 => "one",2=>"two", 3=>"Three" , 4=>"Four"}

puts data

data.delete(9) #when key not exist
puts data

puts data
{1=>"one", 3=>"Three", 4=>"Four"}
{1=>"one", 3=>"Three", 4=>"Four"}
{1=>"one", 4=>"Four"}

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment

© 2021,, All rights reserved