The array toolbox
Changing the shape
Most array methods return a new array and leave the receiver alone. The exceptions carry a bang (map!, uniq!) or are the methods that push and pop. Two of the shape changers are worth knowing by name:
ruby -e 'p [1, [2, [3]]].flatten'
[1, 2, 3]
ruby -e 'p [1, nil, 2, nil].compact; p [1, 1, 2].uniq'
[1, 2]
[1, 2]
flatten goes all the way down; flatten(1) removes one level and keeps the rest. compact drops nil and keeps false, which matters when a list of booleans goes through it.
Set operations on arrays
Three operators treat arrays as sets. They remove duplicates and keep the order of the left side:
ruby -e 'p [1, 2] | [2, 3]'
[1, 2, 3]
ruby -e 'p [1, 2, 3] - [2]'
[1, 3]
ruby -e 'p [1, 2] & [2, 3]'
[2]
Each one is a linear scan under the hood, which is fine for tens of items. For thousands, the Set class, later in lesson 4.11, turns the same operations into hash lookups.
Working at both ends
A stack is a push and a pop; a queue is a push and a shift. Ruby uses one array for both:
ruby -e 'a = [1, 2, 3]; a.shift; a.push(4); p a'
[2, 3, 4]
ruby -e 'p [1, 2].zip(["a", "b"]); p [1, 2].product([:a, :b])'
[[1, "a"], [2, "b"]]
[[1, :a], [1, :b], [2, :a], [2, :b]]
zip pairs items by position and stops at the shorter side; product makes every combination. Both are the quick way to build rows before a each turns them into output.
What to watch for
A method without a bang returns a new array; the receiver is unchanged
unless the name ends in ! or the method is
push, pop,
shift, unshift or
[]=. Assigning the result is the safe habit:
names = names.compact.
| and & compare with
eql? and hash, so two
objects that are equal with == but not with
eql? both survive the union. Lesson 2.6 has the
difference.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
[1, [2, [3]]].flattenreturn?Answer
[1, 2, 3]
flatten goes all the way down, while flatten(1) removes only one level.
-
What does
[1, [2, [3]]].flatten(1)return?Answer
[1, 2, [3]]
One level is removed, and the inner array stays.
-
What does
[1, nil, false, 2].compactreturn?Answer
[1, false, 2]
compact drops nil only; false is a value and stays.
-
What does this program print?
p [1, 1, 2].uniqAnswer
[1, 2]
uniq keeps the first of each repeated value.
-
What does
[1, 2] | [2, 3]return?Answer
[1, 2, 3]
The union keeps the order of the left side and drops the duplicate 2.
-
What does
[1, 2, 3] - [2]return?Answer
[1, 3]
The difference removes every element of the right side.
-
What does
[1, 2] & [2, 3]return?Answer
[2]
The intersection keeps only the elements both sides hold.
-
What does this program print?
a = [1, 2, 3] a.shift a.push(4) p aAnswer
[2, 3, 4]
shift removes the first element and push appends one to the end.
-
What does
[1, 2].zip(["a", "b"])return?Answer
[[1, "a"], [2, "b"]]
zip pairs the items by position and stops at the shorter side.
-
How does
productdiffer fromzip?Answer
product makes every combination, zip pairs by position
Four elements come out of two arrays of two with product, and two come out of zip.
Article author: Arthur Isaev