An array is a built-in Ruby class, which holds a list of zero or more items, and includes methods that help you easily add, access, and loop over all these items.
This is helpful, because if arrays didn’t exist you would have to use many variables.
Example:
a = 1
b = 2
c = 3
But instead, you can do:
numbers = [1, 2, 3]
The best part?
You can put anything inside an array!
Like:
Numbers
Strings
More arrays! (that would make it a multi-dimensional array)
Let’s learn more about arrays so you can make the best use of them!
The map method doesn't modify the array in-place, it just returns a new array with the modified elements, so we need to assign the results back to a variable.
There is a map! (notice the exclamation point) method which will modify the array directly, but in general the simpler version is preferred.
Another thing you may want to do is find all the items in your array that fit certain criteria.
There are a lot of things you can do using arrays, like sorting them or picking random elements.
You can use the sort method to sort an array, this will work fine if all you have is strings or numbers in your array. For more advanced sorting check out sort_by.
numbers = numbers.sort
You can also remove the duplicate elements from an array, if you find yourself doing this often you may want to consider using a Set instead.
Notice that this doesn't permanently change your array. Most Ruby methods will create a new array with the changes you requested. So if you want to save the results you need to use a variable or in this example, use the uniq! method.
If you want to pick one random element from your array you can use the sample method:
numbers.sample
You may also want to "slice" your array, taking a portion of it instead of the whole thing.
Example: Take the first 3 elements from the array, without changing it:
numbers.take(3)
numbers[0,3]
You can do more advanced Ruby array slicing. For example, you may want to get all the elements but the first one:
numbers[1..-1]
Notice the difference between using a comma (0,3) & a range (1..-1). The first one says "get me 3 elements starting at index 0", while the last one says "get me the characters in this range".
If you have two arrays and want to join or merge them into one you can do it like this:
# Faster, because this changes the users array
users.concat(new_users)
# Slower, because this creates a new array
users += new_users
You can also remove elements from one array like this, where users_to_delete is also an array:
users = users - users_to_delete
Finally, you can get the elements that appear in two arrays at the same time:
users & new_users
Conclusion
Ruby arrays are very useful and they will be a powerful ally by your side.
Make sure to practice creating an array, adding elements to it, accessing elements by index, etc. Also read about the Ruby hash, another important class which can be combined with arrays to write more interesting code.
Practice makes perfect!
If you found this useful please share this post & subscribe to my newsletter below ๐
You’ve covered adding elements to the end of an array, and removing them from there, but ignored the front end, which is necessary in order to make a queue. There, you can use shift and unshift, respectively.