-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path20-arrays.rb
93 lines (58 loc) · 2.96 KB
/
20-arrays.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
# Ruby also has two other types of variables, the Array and the Hash
# In this file, we'll explore the Array
# Remember: The Array is a list of values
##########################################################################################
puts("------ Creating and accessing the array ------")
my_friends_favorite_numbers = [13, 17, 21, 35]
my_friends = ["John", "Angie", "Brooke", "Peter"]
# How do we access our values inside the Array? The syntax looks like:
puts my_friends[0] # This will output John
##########################################################################################
puts("------ Iterating the array ------")
# Now we want to go through our entire array and do something with each value
# Let's create an index to point to the first element of the Array
index = 0
# while the index is less than the size of the array (in this case: 4)
# we're also keeping an index as we move through the Array so we can reference the friend's favorite number
while index < my_friends.size() do
friend = my_friends[index] # grab an element from the array and put it into the variable friend
puts "#{friend} is a friend of mine."
puts "#{friend}'s favorite number is #{my_friends_favorite_numbers[index]}"
index = index + 1
end
##########################################################################################
puts("------ Now with each ------")
# This task is so common in coding (going over the elements of an array) that there is a very common shorthand:
my_friends = ["John", "Angie", "Brooke", "Peter"]
my_friends.each do |friend|
puts "#{friend} is a friend of mine."
end
# notice this:
# do |variable|
# .. code ..
# end
#
# We're creating a "block" of code that we'll run for every element in the Array
# For each element, we run that code and copy the value into a temporary variable specified between the |'s, in this case friend
# This makes it so we don't have to create the friend variable ourself and also avoids us having the manage the index
##########################################################################################
# Exercises:
# Explore other methods of Array that are provided by Ruby at http://www.ruby-doc.org/core-1.9.3/Array.html
# Exercise 1:
# Use join (http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-join) to create a single String of my_friends
puts my_friends.join( ", " )
# Exercise 2:
# Use map (http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-map) to lowercase every person in my_friends
my_lowercase_friends = my_friends.map { |friend|
friend.downcase
}
puts my_lowercase_friends
my_lowercase_friends_and_a_number = my_lowercase_friends.map { |friend|
friend + "123"
}
puts my_lowercase_friends_and_a_number
# Exercise 3:
# Use select (http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-select) to select only favorite numbers under 20
puts "============="
puts my_friends_favorite_numbers.select { |number| number < 20 }
puts my_friends.map { |friend| friend.downcase }.join(" -- ")