View my Resume

Writing and referencing Arrays

Writing and referencing arrays may seem scarier than it sounds at first, but once you get the hang of it you will realize that it is easier than it may seem.

Arrays are a group of strings or integers (or even other arrays!) that we can use to reference in our code. For example

array_1 = [1, 2, 3, 4]

You will see here that we have an array of integers defined by a variable array_1 (keep in mind, when writing code, you always separate two words with an underscore).

Now that we have our array set up, we want to reference one of those numbers. Those integers in the array are called elements, and we can reference any of the integers in this array by referencing them in the order they are in (starting with zero, then 1, etc.). For example

array_1 = [1, 2, 3, 4]

array_1[0] => 1
array_1[1] => 2
array_1[2] => 3
array_1[3] => 4

Another type of array that we can use is an array of strings, for example

pets = ["dog", "cat", "turtle"]

Don’t forget, that we reference each element in the array by the order number in which they are. So in this example…

pets = ["dog", "cat", "turtle"]

pets[0] => "dog"
pets[1] => "cat"
pets[2] => "turtle"

One thing to note about arrays is that not only are they good for keeping your code organized, once you learn about loops, creating and referencing arrays will be an excellent tool in creating micro-programs(obviously they won't look great but they work!)

By writing and referencing arrays you can greatly enhance your skills as a blooming coder, and use this skill for more complex code.

A great tool to use when starting with arrays would be ruby-doc. Here is the link to that https://ruby-doc.org/core-2.2.0/Array.html. Good luck on your journey to being a great coder!

Posted: December 10, 2019