Ruby Notes – Ch2 AppAcademy.io Coding Test

The following are my notes from the appacademy.io chapter 2 introduction to programming.

Methods:

  • +
  • *
  • /
  • chomp
  • puts
  • gets
  • to_i
  • to_s

Methods are the verbs of Ruby.

If -> else, and elsif

if num < 10
puts ("That's not a big number.")
end

If a method is true, Ruby will run the method. If a method is false, Ruby will skip it and move to the next method.

Logical Connectives

If you want to define something in which X is true and Y is true use &&

De Morgan’s LawWiki

The negation of a conjunction is the disjunction of the negations.

The negation of the disjunction is the conjunction of the negations.

This is a strange concept because it’s simple when you play out individual cases, but it get strange when you think of it as a whole. If something is not A, it could still be B, but if it’s B it doesn’t mean it’s not A. See, this is a strange way of speaking. I think the ven diagram on the wikipedia page is the best way to describe it.

Anyways, this is a logical tool to be used in more complex programs.

number = gets.to_i
if (number > 10) && (number < 30 )
puts ("Your number is greater than 30 and less than 10.")
else
puts("Your number is between 30 and 10.")
end

Negation is the last logical connector

Use the ! mark to negate.

Infinite Loops

while loops can become infinite loops

How to write an infinite loop

while 1 == 1
puts ("We're looping.")
end

Screen Shot 2015-12-06 at 2.26.59 PM

Aside: after writing this program and executing it, I had to learn quickly how to stop the terminal from creating an infinite loop.

To end an infinite loop using the Terminal press: ctrl + c

Arrays

variable = ["thing1", "thing2", "thing3"]
puts (variable[1])

This code would print thing2

Positioning in the array above:

  • 0 = thing1
  • 1 = thing2
  • 2 = thing3

Important note = arrays start from position 0

Arrays can also be written like this:

random_array = [
"one",
"two",
"thee",
"four"
]
puts(random_array.lenght)
idx = 0
while idx < random_array.length
puts(random_array[idx])
idx = idx + 1
end
puts("Loop Complete")

The above code runs like this:

Screen Shot 2015-12-06 at 2.51.01 PM

Side Note: idx becomes a way of counting. With the above example it will go up linearly until it reaches the number which is greater than the number of arrays in the array. For the example above, it is 4.

Note on idx: using idx is completely arbitrary. Providing you use a constant variable as your means of counting, you can use any collection of symbols. I ran an array using love instead of idx and it worked exactly the same (except larger file size I’m sure because love is 4 characters while idx is three.)

Note on Learning to Code: When learning to code, writing your notes longhand is a waste of time. It’s better to put it into a blog like this. I already feel like I’m gaining a great understanding of the subject matter.

Exercise That is Really Helpful

I was having a really hard time committing the idx value to memory. The key was to do it over and over again. Here’s an exercise to get you practicing coding at a fast level. It’s also a gratitude practice so your killing two birds with one stone.

For the following exercise, do it based off memory. If you can’t do it based off memory and you need to go back to your notes, don’t count that exercise as a success. You want to keep this exercise up until you have 3 consecutive successes with no notes:

Write 3 programs which use idx to systematically list out your arrays. At the end of each array, write, “I think I’m getting this.”

  • Program 1 – Places you Love
  • Program 2 – Foods you Love
  • Program 3 – People you Love

Here’s how my programs went:

Program 1:

Screen Shot 2015-12-06 at 3.17.55 PM

Program 2:

Gratitude Practice and Coding

Program 3:

Screen Shot 2015-12-06 at 4.39.20 PM

After doing this exercise, I know I can write simple Ruby array programs.

Geeking Out on Percentage Change and File Size:

I’m now realizing why programmers so often use really short file names. It’s because we want to type the names of files fast accurately.

In Program 1, I wrote the name of the program to be gp.rb

I could have titled it great_places_that_i_love.rb

…but then I’d be typing 5,400% more per request. <- Note: This is wrong. I’m not doing my statistical calculation right.

short_var = "gp.rb".length
long_var = "great_places_that_i_love.rb".length
puts(short_var)
puts(long_var)
percentage_length = short_var / long_var
puts(long_var.fdiv(short_var))

The above program prints 5.4 which is the percentage of the absolute change in the file name length. <- Note: when I wrote that, I was misunderstanding the rules of statistics.

In order to get the percentage change from a short file name to a long file name as described above, I need to (source) understand the absolute change divided by the number we started out with.

absolute change / starting number = percentage change

I geeked out and wrote a program that will take your short file name and your long file name and return the percentage change in length. Here it is:
puts("Type your short file name now, please.")
short_var = gets.length - 1
puts(short_var)
puts("Type your long file name now, please.")
long_var = gets.length - 1
puts(long_var)
absolute_change = long_var - short_var
puts(absolute_change)
ac_div_startnum = absolute_change.fdiv(short_var)
puts("Percetage change: ")
puts(ac_div_startnum * 100)

Though this was a fairly silly exercise, I really feel like I understand Ruby more than ever.

That’s It

That concludes my notes for chapter 2. If you’d like to continue on with my notes, please select from more options below.

Chapter 1 notes can be found here.

Chapter 3 notes can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.