What is an array and a hash?
In the most simplest term, they are just a container to put things into.
What is the main different between them?
It depends on how we want to organize our information. Order or non order format, list or key/value format. Array always come in square bracket [] and hash comes in curly bracket{}.
In what situation do we either of them?
Array is more like a list format, just keep adding items to the list. Just like hash it doesn’t matter what data type is added, numbers, strings, booleans, functions, variables, hashes, or other arrays. The only thing it keeps track of is the order that is added in. We call this numbering system, its index number. It starts from 0. If there is a count of 5 items in the list, we have the last index being at 4. Index number = total count(total length)-1. Some situation includes, do we have this item in our container? How many of a certain item do we have? What is the greatest or lowest value do we have?
Hash is a key and value pair. It doesn’t matter when it was placed inside the container. All we care about is the value that is behind each key. Key represents what type of information that is being held and the value is the data. A simple example a 1 to 1, place: Queens / like ice cream?: yes / school: Flatiron. A more complex example would be, key: person’s name and the value(s) could be an array of names or it can be another hash, with further detail having keys’ of male and female and it value, a list of names.
Basic methods
There are many pre build methods for each type of data. A simple way to show us what is available for us to use, just type in
Array.methods #a list of methods for an array
Hash.methods #a list of methods for a hash
The most often used methods are each, select, find, length, and include?
Each is the broadest enumerable that cover all the other enumerable. It iterate through each item in an array or each key and value pair in a hash.
Select will return a sub array or sub hash where a condition is met.
Find will return the first item or key/value that satisfied the condition.
Length will return the number of item in the container.
Include? will return either true or false, if the condition is met or not.