Skip to content

Commit 5856c47

Browse files
authored
any all none find
Ruby Enumerables: 'any', 'all', 'none', and 'find'
1 parent af62387 commit 5856c47

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def func_any(hash)
2+
# Check and return true if any key object within the hash is of the type Integer
3+
# If not found, return false.
4+
hash.any? { |k, i| k.is_a? Integer }
5+
end
6+
7+
def func_all(hash)
8+
# Check and return true if all the values within the hash are Integers and are < 10
9+
# If not all values satisfy this, return false.
10+
hash.all? { |k, v| v < 10 }
11+
end
12+
13+
def func_none(hash)
14+
# Check and return true if none of the values within the hash are nil
15+
# If any value contains nil, return false.
16+
hash.none? { |k, v| v == nil }
17+
end
18+
19+
def func_find(hash)
20+
# Check and return the first object that satisfies either of the following properties:
21+
# 1. There is a [key, value] pair where the key and value are both Integers and the value is < 20
22+
# 2. There is a [key, value] pair where the key and value are both Strings and the value starts with `a`.
23+
hash.find { |k, v|
24+
(k.is_a? Integer and v.is_a? Integer and v < 20) or
25+
(k.is_a? String and v.is_a? String and v[0] == 'a') }
26+
end

0 commit comments

Comments
 (0)