Error:

Create empty hash

# literal
h = {}
# Constructor
h = Hash.new

Create hash with elements

h = {:key => 'value1', '23' => 'value2'}

Add element to the hash

h = Hash.new
h['name'] = 'Alex'
h # => {"name"=>"Alex"}

Check if hash is empty

h = {}
h.empty? # => true

Check if key is present in the hash

h = {:name => 'Alex'}
h.has_key?(:name) # => true
h.has_key?(:last_name) # => false

Check if key is present in the hash

h = {:name => 'Alex'}
h.has_value?('Alex') # => true
h.has_value?('John') # => false

Delete key from hash

h = {:name => 'Alex'}
h.delete(:name)
h # => {}

Iterate over all keys of the hash

h = {:name => 'Alex', :title => 'Mr'}
h.each_key { |k| puts "Key: #{k}" }

Iterate over all pairs of the hash

h = {:name => 'Alex', :title => 'Mr'}
h.each { |k,v| puts "Pair: <#{k}, #{v}>" }

Merge two hashes

h = {:name => 'Alex', :title => 'Mr'}
info = {:age => '33', :degree => 'Bachelor' }
h.merge(info) # => {:name=>"Alex", :title=>"Mr", :age=>"33", :degree=>"Bachelor"}
h # => {:name => 'Alex', :title => 'Mr'}
h.merge!(info) # => {:name=>"Alex", :title=>"Mr", :age=>"33", :degree=>"Bachelor"}
h # => {:name=>"Alex", :title=>"Mr", :age=>"33", :degree=>"Bachelor"}

Find common keys of two hashes

person = {:name => 'Alex', :title => 'Mr'}
info = {:age => '33', :degree => 'Bachelor', :title => 'Mr' }
common_keys = person.keys & info.keys # [:title]