Arrays, Hashes, and Other Groups of Data
Ruby collections refer to arrays or hashes, two ways to store “collections” of data. Arrays are simple lists, and hashes are lists of key-value pairings.
Making Quick Collections
There’s several syntaxes for quickly making collections without getting bogged down in detail.
Collections and Methods
This makes it simpler to add anything to an array as needed. It also gives you more control over creating, managing, and deleting from arrays, since they can all be linked to different methods with parameters as needed.
There’s also a good shortcut for using hashes as method arguments. If a hash is the method’s last argument, you don’t need to wrap the hash in braces. You can go right to the hash rocket syntax.
Looping Through Collections
each instead of
for basically calls the
each method itself, creating an avoidable inefficiency.
You can do the same thing for hashes, and by default each result is a two-item array. The first item is the key or symbol, and the second is the value. You can also explicitely name the pairs to destructure them.
Useful Collection Methods
sort rearranges an array based on item values. It takes the value of numbers, and uses the length of a string for those values, and arranges them in ascending order. You can add
reverse onto these to quickly go into descending order.
To sort by different measures, you can use
sort_by which takes a block. This block can compute a different numerical value in any way, and the array will be sorted on those returned values
Loops through an array, and returns the index of the first object that it’s true for. Can either directly look for a value, or take a block statement for more complex checking.
You can use
rindex for the same check, but starting from the end of the array instead.
map takes an array, performs a function or operation on each item, and returns a new array.
inject is like a normal array, but with a few differences:
- The loop takes two variable declaration. The first is the same as before, the current item in the loop. The second is a value that’s carried over from the previous iteration, which I call the “carried variable.” Each one can be used in the loop’s code.
- The return value replaces the carried variable in each iteration
- It also takes an argument when called, which is the initial value of the carried variable. If you don’t include this, it instead does:
- Takes the first array value as the initial value.
- Skips the first iteration and continues as normal from the second onward.
A common example of this is finding the sum of an array of numbers.
push, delete, pop, shift
These four methods change the array they’re called on in some way. Note these change the variable themselves, instead of limiting the change to a new variable. Use them with caution!
pushadds an item to the end of an array, and returns the changed array (with added elements). It can take multiple arguments, each of which are added in that order.
deletedeletes any items from an array that matches the given value (can be multiple). If it finds no match, it returns
nil. It can also take a block, which will run instead if there’s no match.
popremoves the last element of an array, and returns the removed element. It can take a numerical argument, such as
pop(2), to remove multiple items at an array’s end.
shiftdoes the same as
pop, but starting from the start of the array.
The above four methods (and likely several others) natively can change the same variable they call. Other methods, such as
reverse, don’t ever change the actual array they’re called on. Use them on an array and call it again, and it will be the same.
Use the bang operator, however, and the array itself will change. So be cautious with the bang!