The following are my notes from the appacademy.io chapter 3 introduction to programming.
Adding and/or Removing from Arrays
- push – adds elements to the end of an array
- unshift – adds elements to the beginning of an array
- shift – removes an element from the beginning of an array
- pop – removes an element from the end of an array
while cool_things.length < 3
puts("Tell me something cool.")
one_cool_thing = gets.chomp cool_things.unshift(one_cool_thing)
end
puts("Here are your cool things in order:")
idx = cool_things.length - 1
while idx >= 0
puts(cool_things[idx])
idx = idx - 1
end
The above code asks us to do things in order:
- Limit array – while…
- Ask for input – puts…
- Record input to array – gets..
- Instructs to add input to top of array – puts(cool_things[idx])
- Ends the array – end
This prints the three things the user enters in order. How to have users generate an array without knowing how to type the array code ( variable = [“1”, “2”, “3”] ) *This would be a useful tool if you wanted to create arrays quickly without having to type the quotations so often.
Here’s a screen shot of defining the difference between push and unshift. The key in seeing the difference is to look where the code reads, “Here are your cool things in order:”. It does this twice and they are different because push adds the variable to the beginning of the array and unshift adds it to the end.

Side Note: puts(“ringo”.length) #puts integer 5 | Strings can become arrays
Side Note #2: If you type “clear” into the terminal and press enter, it clears the screen. Wish I had known about that a few hours ago….
ran_num = [1, 2, 3, 4]
puts(ran_num[1])
#Puts the number 2
Update: I’m having a hard time committing this to memory so I’m doing the same memory commitment process I used in the notes for section 2. Essentially, I’m writing programs that ask me 3 things I love and then spit the responses back to me in order.
COMMITTING CONCEPTS TO MEMORY EXERCISE: The idea here is to write each program as well as I can without any outside help from blogs, the internet or the course work.
Once the program is written to the best of my ability, I’ll run it to see if I got it right. If I didn’t get it right, I’ll use the material to fix up the code. Then I’ll try again. I’ll keep trying to write the code until I get a program right all on my own. Then I’ll do it two more times without error.
The hope is that this commits the process to memory… but most valuably, it forces me to nail down specific reasons and connections between each line of code.
- Program 1 – Favorite Song
- Program 2 – Favorite Material
- Program 3 – Favorite Metal
I’m confident I understand this concept of writing code that records a purposeful number of inputs and then can read them out either in order or backward order.
Removing from Arrays:
This is straight forward. I feel the best way to describe this is to show two examples:
arr = [2, 3, 5, 7]
item = arr.pop
puts(arr)
prints: 2 3 5
arr = [2, 3, 5, 7]
item = arr.shift
puts(arr)
Prints: 3 5 7
Setting Positions in an Array
This section is fairly straightforward. Positioning in an array seems like it will play an important part in the ability to code.
arr = [1, 2, 3]
arr[1] = "Two"
puts(arr == [1, "Two", 3]
This code shows that you can assign a string to a single part of the array and it will print true when run.
Nil
The manual makes it known that if you ask Ruby to print a value that isn’t stored in an array, Ruby will just print nothing (we call this “nil.”)
Strings are Like Arrays
Just like with arrays, you can print individual elements of a string:
hello_champ = "Champion"
puts(hellow_champ[3])
#prints m
Note: I understand this on a core level so it’s easy to commit to memory. It’s easy for me to read this next code, but it isn’t something I feel like I could write from memory. Check it out:
string = "12345"
idx = 0
while idx < string.length
puts(string[idx])
idx = idx + 1
end
For the above code, I practiced the same memory commitment exercise. I’ll reiterate because it’s been so valuable. I would make up a variable and a string (example = wife = “Veronica”), then I would write the code so that the computer would spell out the individual variables one by one.
wife = "Veronica"
idx = 0
while idx < wife.length
puts(wife[idx])
idx = idx + 1
end
This prints out each letter of Veronica’s name on a separate line. To commit this to memory, I would write a program like this from memory and run it. It took me 2 tries before I could do it from memory. After I got it right without any errors, I did it again and again once more. Once I could do it three times without a mistake, I feel like I can move on as this has been committed to memory.
puts("123" == ["1", "2", "3"])
Just because strings can act similar to arrays, they aren’t exactly the same. The above code works in what it prints out, but if you set them equal, Ruby prints false.
Joining Arrays and Splitting Strings
puts("hello my friend".split)
This prints:
hello
my
friend
This is a quick way to break strings into arrays.
friends = ["Friends ", "are ", "good ", "to ", "have."]
friends_string = friends.join
div_friends = friends_string.split
puts(friends == div_friends)
This prints false, even though it returns an array that was converted to a string and then back to an array. This is the above lesson in which we learn that they are similar, but not the same.
friends = ["Friends ", "are ", "good ", "to ", "have."]
friends_string = friends.join
div_friends = friends_string.split
puts(div_friends.join(" "))
puts(friends.join)
puts(friends == div_friends)
For me, the best way to learn the nuances of the code is to play around with different ideas. split and join make more sense the more I play with them. In the above code, I created an array, joined the array as a string, saved that as a variable, then split the string variable back into and array.
I then printed the initial array and the final array which was converted to a string and back to an array. Even though they print the same, if you ask Ruby if they are equal, it says false. This is an interesting nuance of the language which I would love to hear an explanation for. That is for another time…
Writing your Own Methods
We want to write our own methods to repeat code multiple times.
def cube_numbers(how_many)
var1 = []
idx = 0
while idx < how_many
var1.push(idx * idx * idx)
idx = idx + 1
end
return var1
end
puts ("How many cubed numbers do you want?")
wanted_cubes = gets.to_i
var1 = cube_numbers(wanted_cubes)
idx = 0
while idx < var1.length
puts(var1[idx])
idx = idx + 1
end
While writing the above method, I decided to make it cubed instead of squared like in the example with AppAcademy.io. I also changed all the variables so that while I was typing it out, I had to be very conscious of the connecting variables.
This was important in understanding the code, but I’m not sure that I got it a hundred percent. I can’t write this method system from memory yet.
I’m tempted to move forward without going through the exercise of rewriting it again and again, but the truth is that I need to do this. Methods will be a critical aspect of programming in the future and I need to know them from memory.
Update: This has taken me a while to remember, but I’m even more confident that this is critical to being good at coding. I’ve rewritten the code 4-5 times but still make mistakes. I’ll continue to do so until I can write the blow three programs from memory/understanding.
December 13th, 2015: Still going slow. I’m working to understand the language well enough to complete the practice tests. The day before yesterday, I was able to take the practice tests apart and learn the pieces that went into them for the first time. Before then, the practice test was a foreign language to me. This is slow, lethargic progress, but it is progress none the less. Today I’ll wrap up the notes on this Chapter 3 and get back into the practice test.
Update 2: Dec 14, 2015 – This has taken a long time to remember. There are a few things going on in this code.
- Define a process to work numbers and return them
- Requesting how the numbers to be returned
- Using the request to create an array of requested numbers
- Redefining the process to print the array
Update 3: Dec 15, 2015 – Though this has taken a few days to get good at, I’ve not been working on just this. I’ve been working through methods on Codecademy and reading and taking notes on Ruby in 100 Minutes. All this cross reference is helping a lot. When I get stuck on one thing (like remembering this) I go around and re-learn concepts put forth in different ways. This helps because hearing the same thing in different ways is excellent for cementing knowledge. I just sat down and for the first time, can write the squares code of this example with no errors. So I’m ready to write out 3 versions of it (singles, trips and cubes) and then I’ll feel confident with creating methods and puts the number of outputs.
Program 1 – Repeating the Requested Single Numbers
Program 2 – Tripling the Requested Numbers
Program 3 – Cubing the Requested Numbers
Note: I slowed this way down to get this method definition and relaying of set numbers syntax committed to memory. Over the course of doing this, I’ve been checking in with the AppAcademy practice tests and I learned that the whole practice test is essentially creating def that make the readout codes work. Therefore, it’s clear that having a powerful grasp of the above concept was critical for passing the tests in the future. Even though this took about 3 days to commit to memory, I feel like it’s been great for my core understanding of how it all works.
Breaking Out of Loops
Ok. Loops will be the same process as above. The good news is that it’s much quicker to learn because the loops code is about 9 lines whereas the code for writing methods was about 19.
Program 1 – What’s the center of the Solar System?
Program 2 – First name of the President of the USA?
Program 3 – the most populous city in the USA
sidenote: though tedious, this repetition is critical. I’m ironing out a lot of kinks with each repetition. Also, I’m learning to correct things. Just a second ago I wrote “get.chomp”, when I ran this through terminal, terminal showed me that it was wrong and I self-corrected. Good times.
Returning Early
Again, a pair of concepts that were much easier to repeat and commit to memory.
def smallest_square(lower_bound)
i = 0
while true
square = i * i
if square > lower_bound
return square
end
i = i + 1
end
end
puts(smallest_square(60))
The above code was interesting because it displayed a code that was interesting but not very useful. It asked for the lower bound of a number that if squared would equal something. So you input the number (60 in the above example) and it feeds you back the number that, once squared, would be above the lower bound. This is useless unless you could find the lower bound number to be squared right? Right.
So I added a request to print n at each attempt and the code outputted:
1
2
3
4
5
6
7
8
64
That tells em that 8 is the lower square that will devise into 64 which is the lower bound. This was a cool learning bit about where we can type in code to give us different information that can be useful.
That’s it for my Notes On AppAcademy intro to Coding.
If you found these useful, check out my notes for chapter 1 or chapter 2. The source material for this chapter can be found on the appacademy.io site here.
How I Setup my Coding Workspace and Note Taking on WordPress:
My code workspace for this note-taking process is set up with WordPress as my note screen, this site as my learning material, the Terminal for executing programs, and Atom for writing and saving code. I keep a coding folder on the Desktop and call it aawork so it’s easy to find and open. Here’s what my screen looks like:
That’s It
Now it’s time to do the next step. This introduction was quite valuable and I feel I learned a lot from it. Truth is, I feel like I just opened up a door. I’m realizing that there is a whole complex, and interesting world behind it.
My objective is simple right now, to get into AppAcademy later in the year (2016). I don’t need to learn to program now, I just need to learn how to do well on the AppAcademy coding challenge.
If you want more notes like this, please feel free to check my notes for chapter 1 or chapter 2. I’ll probably be making < TK – Enter link to notes here > notes on the Introduction to Programming Summary (source material) too.
It’s hard to know how my notes will go as I work through the practice problems because they are on a different platform (c9.io) which may have a system for organizing though native to the platform. I’ll know more as I move through the notes.
Until then, I’ll use this blog to keep track of my progress. I’ll be taking my own notes on ruby resources and this will be a part of the breadcrumb trail from chapter 1, to the most recent of my Ruby studies and notes.
For the next breadcrumb in this series, please feel free to move onto my notes on Ruby in 100 Minutes (here is the source material). This is a page of notes I’m taking on a piece recommended by AppAcademy.