-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashes.rb
More file actions
68 lines (52 loc) · 1.47 KB
/
Copy pathhashes.rb
File metadata and controls
68 lines (52 loc) · 1.47 KB
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
#Hashes: 1
family = {
uncles: ["bob", "joe", "steve"],
sisters: ["jane", "jill", "beth"],
brothers: ["frank", "rob", "david"],
aunts: ["mary", "sally", "susan"]
}
keys_to_extract = [:sisters, :brothers]
immediate_family = [family.select {|k,_| keys_to_extract.include? k }]
puts immediate_family
#Hashes: 2
"Merge method = creates a new hash where the latest hash values are added to the new, but keeps the old hashes"
"Merge! method = adds content from old hash to new, and overwrites the values - so old hash is changed"
#1. Merge
h1 = {"a" => 1,"b" => 2,"c" => 3}
h2 = {"c" => 4,"d" => 5}
h1.Merge(h2)
puts h1
puts h2
#2. Merge!
h1 = {"a" => 1,"b" => 2,"c" => 3}
h2 = {"c" => 4,"d" => 5}
h1.Merge!(h2)
puts h1 #is now changed
puts h2
#Hashes: 3
person = {
:name => "Felix",
:age => 33,
:gender => "Male"
}
person.each do |keys,values|
puts "#{keys}:#{values}"
end
#Hashes: 4 + 5
person1 = {
:name => "Bob",
:occupation => "web developer",
:hobbies => "painting"
}
puts person1[:name]
puts person1.detect{|name| name.include? ("Bob")}
#Hashes: 6
words = ["demo", "none", "tied", "evil", "dome", "mode", "live", "fowl", "veil", "wolf", "diet", "vile", "edit", "tide", "flow", "neon"]
h = words.group_by {|word| word.split("").sort}
h.each do |_,values|
puts "#{values}"
end
#Hashes: 7
x = "hi there"
my_hash = {x: "some value"} #The key is here a symbol
my_hash2 = {x => "some value"} #The key is here a variable and relates to "hi there"