|
59 | 59 | veggies.push "Onions"
|
60 | 60 | puts veggies.last, "\n"
|
61 | 61 |
|
62 |
| -some_array = %w[a b c d e f g] |
| 62 | +some_array = %w(a b c d e f g) # short syntax for array deifnition - %w() |
63 | 63 | puts "some_array: #{some_array.join(", ")}"
|
64 | 64 | puts some_array[-3, 2].join(", "), "\n"
|
65 | 65 | # The reason this gives us "e, f", is because: while -3 tells Ruby where to start looking in the array
|
|
83 | 83 | # element in the array means they are not found. And so, nil is returned.
|
84 | 84 | # puts a_combi.assoc(2).join(", ")
|
85 | 85 | # puts a_combi.assoc("blue").join(", ")
|
86 |
| -# puts a_combi.assoc("b").join(", ") |
87 |
| - |
88 |
| -puts |
| 86 | +# puts a_combi.assoc("b").join(", "), "\n" |
89 | 87 |
|
90 | 88 | # Here's some interesting stuff - regarding using %w to create an array:
|
91 | 89 | a1 = %w[red green blue]
|
|
99 | 97 | puts a_combi.assoc("red").join(", ")
|
100 | 98 | # %w does NOT make [1 2 3] into [1, 2, 3]...
|
101 | 99 | # it DOES, however, make ["1", "2", "3"] - testing it:
|
102 |
| -puts a_combi.assoc("1").join(", ") |
| 100 | +puts a_combi.assoc("1").join(", "), "\n" |
103 | 101 | # And now it is found - huzzah! No nil.
|
104 | 102 |
|
105 |
| -puts |
106 |
| - |
107 | 103 | # Playing around with multiple arrays that start with a given element:
|
108 | 104 | a1 = %w[orange banana cactus]
|
109 | 105 | a2 = %w[leemur hippo kitten]
|
110 | 106 | a3 = %w[orange roflsaurus cheeseburger]
|
111 | 107 | a_combi = [a1, a2, a3]
|
112 | 108 | puts a_combi.join(", ")
|
113 |
| -puts a_combi.assoc("orange").join(", ") |
| 109 | +puts a_combi.assoc("orange").join(", "), "\n" |
114 | 110 | # The array 'a1' is returned, as it is the first array (found) that starts with the given element (orange).
|
115 | 111 |
|
116 |
| -puts |
117 |
| - |
118 |
| -# Using the previous example (lines 126-133): |
| 112 | +# Using the previous example (lines 126-133): testing to ensure we are infact returning a1 |
119 | 113 | puts a_combi.assoc("orange") == a1
|
120 | 114 | # We see that the array is returned, as the above returns true. However...
|
121 |
| -puts a_combi.assoc("orange").join(", ") == a1 |
122 |
| -# ...this one returns false - note the usage of .join. |
123 |
| - |
124 |
| -puts |
| 115 | +puts a_combi.assoc("orange").join(", ") == a1, "\n" |
| 116 | +# ...this one returns false - note the usage of .join. which adds the comma. |
125 | 117 |
|
126 | 118 | # .collect - essentially an each-do for modifying all elements in an array:
|
127 | 119 | a1 = %w[a d ez]
|
|
0 commit comments