-
Hashes–Another Data Structure!!! [Ruby Programming Language Notes]
Hashes! (Another data structure) An array allows us to have a single variable, or location, to store & group a lot of data. (Allows for organization!) An array was the first data structure we learned about. [ ]–An array is made up of elements, organized by indices. But sometimes we may need a different organization when building a program. That’s why we have HASHES!!! A hash is made up of values stored by keys. (A key “unlocks” the corresponding value.) { }–curly braces are used to represent a hash in Ruby. (This can be assigned to a variable.) In a hash, data comes in a pair, (a ‘key value’ pair).…
-
Enumerables–A Cleaner Way to Iterate!!! [Ruby Programming Language Notes]
ENUMERABLES Enumerables–a cleaner way to iterate. Enumerable Methods–a way to quickly iterate thru an array or string array. array .each The ‘.each’ method takes in a {block-of-code} instead of (parameters). ‘.each’ passes on just the element. {block-of-code}–uses curly brackets{} to represent it. .each_with_index The ‘.each_with_index’ passes on the element, with the index, to the block of code. string .each_char .each_char.with_index Range enumerable–allows us to control where we start iterating & where we stop. (start..end).each iterate from start to end (inclusive!) (start…end).each iterate from start to end (excluding end!) A range can also be used on letters & other patterns, not just numbers!) Array Syntax for it: months = [ “Jan”,…