-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-05-enumerable.rb
115 lines (84 loc) · 2.09 KB
/
01-05-enumerable.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Examples
[2, 3, 1, 0, 5, 6].sort
people = [["Bob", 24],["Dave", 26],["Zayn", 22]]
people.sort_by { |person| person.last }
array = [1,2,3,4,5]
# instead of
result = []
array.each do |num|
result << num if num.odd?
end
result
# use `select` or `find_all`
array.select { |num| num.odd? }
# or even better
array.select(&:odd?)
# similar with `map` or `collect`
# so instead of what we did in lines 7-13 with `each`, we can
array.map { |n| n * 2 }
# or
array.map(&2.method(:*))
# `find` and `detect` do the same
# returns the first item for which the block returns a non-false value
[1,2,3,4,5].find do |num|
num.even?
end
[2, 3, 1, 0, 5, 6].max
people = [["Bob", 24],["Dave", 26],["Zayn", 22]]
people.max_by { |person| person.last }
[2, 3, 1, 0, 5, 6].min
["selena", "carly", "justin"].min
people.min_by { |person| person.last }
[1,1,1,1].all? { |num| num == 1 }
[1,1,1,1].all? { |num| num.eql? 1 }
words = ["dog","cat","pig","hippopotamus"]
words.all?
words.any? { |word| word.length == 3 }
words.none? { |word| word.length == 2 }
words.none? { |word| word.length == 3 }
words.one? { |word| word.length == 3 }
array = [1,2,3,4,5]
array.reduce(0) { |sum, n| sum += n }
# Shorthand version does exactly the same
array.reduce(:+)
# In case of summation, then Array has the `.sum` method
array.sum
# number of items in the array. Same as `.length`
array.count
array = ["cat", "fish", "corgi"]
array.group_by { |string| string.length }
a = %w(1 2 3)
b = %w(a b c)
a.zip(b)
# Activities
# 1. What do you think the output of the following will be? And why?
[1,2,3,4,5].map do |n|
n * 2
puts "Hi!"
end
# 2. Given two associated arrays.
people = [
"Hannah",
"Penelope",
"Rabastan",
"Neville",
"Tonks",
"Seamus",
]
houses = [
"Hufflepuff",
"Ravenclaw",
"Slytherin",
"Gryffindor",
"Hufflepuff",
"Gryffindor",
]
# Write a script with the following output:
#
# "Hannah is in Hufflepuff."
# "Penelope is in Ravenclaw."
# ...and so on and so forth.
# 3. Check out
# [Enumerable's docs](http://ruby-doc.org/core-2.4.1/Enumerable.html)
#
# What methods haven't we covered yet? Any of interest?