-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_hash.rb
83 lines (61 loc) · 1.29 KB
/
ruby_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
#!/usr/bin/ruby
users = ["hello","mike","kumar"]
# hash are also know as objects in other languages
car = {
"make": "Ferrari",
"modle": "812 super",
"seats": 2
}
puts car
#it will notwork because we have to use "=>" insted of :
#puts car["make"]
# the code blew will work same as with "make"
car2 = {
:make => "Ferrari",
:modle=> "812 super",
:seats=> 2
}
#puts car2[:make]
# list of hases
cars = [
{:make => "Ferrari",
:modle=> "812 super",
:seats=> 2},
{:make => "Fe",
:modle=> "812 super",
:seats=> 2},
{:make => "Ferrari",
:modle=> "812 r",
:seats=> 2}
]
#puts cars
#we can use loop to access the elemenst
cars.each do |car|
puts car[:make]
end
#nesting the hash
movies = {
:name=> "deadpool",
:characters => ["Deadpool", "spiderman", "wolvrine"]
}
#now we can use loop to accesss the values
# movies[:characters].each do |name|
# puts name
# end
# using key and values method to show objects and the data
#this will display everything
# for i in movies
# puts i
# end
#this will display objects name
# for i in movies.keys
# puts i
# end
#this will display objects contents
for i in movies.values
puts i
end
#this will display specific content using 0= key and 1 = value
for i in movies
puts i[1]
end