It was brought to my attention recently that I don’t have a solid enough grasp on the fundamentals of Ruby. This is contributing to a lot of confusion on my part while pairing. As a way to combat this obstacle, I am going to pause on my projects for a couple of weeks and focus on all the exercises on Ruby Koans. This will definitely help me in the programming confidence department, and also allow me to better comprehend whats happening in a Rails application. It would be mighty difficult to try and read a book without being rock solid in first processing words, and eventually being able to gather meaning from paragraphs.
So here I go!
Today I wanted to briefly cover arrays and parallel asignment
About Array Assignments
I first learned this from a book titled The Well-Grounded Rubyist, and as I have never used it in production, thought that it would be a good way to start off.
We can assign variable names to array indexes like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# variable stores an array with two indexes | |
array = ["Hello", "Kitty"] | |
#two variables, each storing the value of an index from the array | |
first_index, second_index = ["Hello", "Kitty"] | |
first_index # => reuturns "Hello" | |
second_index # => returns "Kitty" |
Line 2 is how an entire array can be stored into one variable, and line 5 illustrates a way to store specific indexes into multiple variable names using one line of comma separated variables for index-by-index assignment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buick, chevy, *audi = ["Park Avenue", "Impala", "Q8", "A5"] | |
buick # => returns "Park Avenue" | |
chevy # => returns "Impala" | |
audi # => returns ["Q8", "A5"] |
Both the buick
and chevy
variables contain values for the [0] and [1]
indexed array values respectively. In the case above, the * or splat operator
before our audi
variable means that *audi
will collect the remaining values of the original array and store them as a new array.
Trying this technique with three variables and an array of only two indexes can be done, but the value stored in the third variable will be nil
.
Here’s an example of this in action:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hungry_bob, hungry_jack = ["Will receive plenty of food"] | |
hungry_bob # => returns ["Will receive plenty of food"] | |
hungry_jack # => returns nil |
We can also assign sub-arrays with this technique like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
college_grad, post_graduate = ["bachelors degree", ["bachelors degree", "masters degree"]] | |
college_grad # => returns "bachelors degree" | |
post_graduate # => returns ["bachelors degree", "masters degree"] |
This technique works no matter which index the sub-array is contained in.
I hope that these illustrations have offered as much insight to you as they have for me.
Categories: AirPair, Newbie, Ruby
Tags: airpair.com, newbie, Ruby