-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ruby_Tutorial_HASH.rb
89 lines (67 loc) · 1.53 KB
/
Ruby_Tutorial_HASH.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#Hash in Ruby
States = {"WestBengal"=>"WB",
"Karnataka"=>"KA",
"Punjab"=>'PB'}
print(States)
puts()
india = {
South:["Chennai","Karnatka","Kerala"],
North:["Delhi","Kashmir","Punjab"],
East: ["WestBengal","Assam","Mizoram"],
West:["Mumbai","Gujrat"]
}
print(india)
#Acccess Values by keys
puts()
print(States["WestBengal"])
Student_List = {}
Student_List["John"]= 23
Student_List["Kevin"]= 22
puts()
print(Student_List)
#Merge two Hashes
new = States.merge(Student_List)
puts()
print(new)
puts()
print(States.keys())
puts()
#fetching Values by Keys
print(States.fetch("WestBengal"))
puts()
#Handling Fetch Fails
print(States.fetch("Jammu","No Key"))
puts()
#Looping through Hash
india.each {|x,y| puts("#{x}:#{y}")}
puts()
#Finding Frequency of a String
word = "hippopotomonstrosesquippedaliophobia"
frequency = Hash.new(0)
for letters in word.split("")
frequency[letters] = frequency[letters]+1
end
print(frequency)
puts()
#Different way to Form HASH
fruits = {apple:2,banana:3,pineapple:4}
puts(fruits)
fruits.each_key do |x|
puts("The KEY #{x} is a #{x.class}")
end
puts()
fruits.each do |x,y|
puts("The KEY #{x} is a #{x.class}")
puts("The VALUE #{y} is a #{y.class}")
end
#Interchange KEY VALUE
puts("---------------------------------------------")
puts("Interchanging the KEY and VALUE of an HASH")
puts("---------------------------------------------")
puts()
new_fruit = fruits.invert
print(new_fruit)
new_fruit.each do |x,y|
puts("The KEY #{x} is a #{x.class}")
puts("The VALUE #{y} is a #{y.class}")
end