-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.rb
54 lines (44 loc) · 1.02 KB
/
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
h1 = {'key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3', 'key4'=>'val4'}
h2 = {'key1'=>'val1', 'KEY_TWO'=>'val2', 'key3'=>'VALUE_3',
'key4'=>'val4'}
p( h1.keys & h2.keys )
p( h1.keys+h2.keys )
h2 = Hash.new("Some kind of ring")
p( h2)
# hash to array
def artax
a = [:punch, 0]
b = [:kick, 72]
c = [:stops_bullets_with_hands, false]
key_value_pairs = [a, b, c]
Hash[key_value_pairs]
end
p artax
# Exercise
family = { uncles: ["bob", "joe", "steve"],
sisters: ["jane", "jill", "beth"],
brothers: ["frank","rob","david"],
aunts: ["mary","sally","susan"]
}
family = family.select do |k, v|
k == :sisters || k == :brothers
end
p family
arr = family.values.flatten
p arr
#exercise 2
words = ['demo', 'none', 'tied', 'evil', 'dome', 'mode', 'live',
'fowl', 'veil', 'wolf', 'diet', 'vile', 'edit', 'tide',
'flow', 'neon']
result = {}
words.each do |word|
key = word.split('').sort.join
if result.has_key?(key)
result[key].push(word)
else
result[key] = [word]
end
end
result.each_value do |v|
p v
end