Day 14

I promise a good blog post soon. It’s almost 1 am after a LONG pair programming session, thanks Mike! We did, however, figure out a hard lab. I would’ve been struggling with it for sure. It’s not as ambiguous as it could be and we didn’t break down our methods as much as we could. However, we got all 15 tests passing and learned some new methods along the way. Specifically, the Dir class methods. Mike also showed me that there’s a solution branch to all of the labs on GitHub which I never knew about. I guess it says something that I’ve made it so far so quickly and never looked at a solution. However, there can be knowledge gained from refactoring with new knowledge based on what the school thinks is the best code for the problem.

I do have “homework” from Mike too (some things he has learned recently that I’m not knowledgeable on). Specifically, ||= &. and .tap.

Time spent today: 11:09
Time spent total: 67:43
Lessons completed today: 11
Lessons completed total: 258

Day 13

I’m keeping the update short and sweet tonight. I got A LOT done today. However, my amazing wife has let me code all day and into the night on her birthday. Anything important I took notes on today I’ll throw in tomorrow’s post. It’s time to spend some time with her.

Time spent today: 9:15
Time spent total: 56:35
Lessons completed today: 13
Lessons completed total: 247

Day 12

Today didn’t see much coding. Mostly because the Intro to Object Oriented Ruby is a 1:10.00 video by of Avi teaching the basics of OO Ruby. Which was good. I found that him talking through concepts made them more clear as I worked on just a couple labs afterwards. It also meant that I took some notes on things that I thought were most important.

Notes which of course I’ll share:

self is the implicit receiver of all non-explicit method calls.

also

local variable is variable
instance variable is @variable
class variable is @@variable

and

attr_reader macro creates getter method
attr_writer macro creates setter method
attr_accessor macro creats both setter and getter method

Avi also talked about how he personally only used the attr_accessor macro when he’s building stuff. Then after everything is working he takes away abilities depending on what the methods actually are doing. The attr_accessor macro is the most flexible so it makes sense to use that by default.

Time spent today: 2:05
Time spent total: 47:20
Lessons completed today: 4
Lessons completed total: 234

Day 11

Today was strong work. I finished Procedural Ruby! Now I have Object Oriented Ruby which has final projects and I’m done with the Ruby section! I have to say it started to feel like I wasn’t making progress but the last two days have reinvigorated me. I know it gets more complex as I go and the projects get bigger and take longer but the groove is starting to fall in place.

I spend a good amount of time on the second to last lab of Procedural Ruby (there is a small RegEx section that is last). One specific part was hard but I was stoked with the elegant (so I think) answer I was able to come up with. My original plan was NOT this.

# given that holiday_hash looks like this:
# {
#   :winter => {
#     :christmas => ["Lights", "Wreath"],
#     :new_years => ["Party Hats"]
#   },
#   :summer => {
#     :fourth_of_july => ["Fireworks", "BBQ"]
#   },
#   :fall => {
#     :thanksgiving => ["Turkey"]
#   },
#   :spring => {
#     :memorial_day => ["BBQ"]
#   }
# }

def all_supplies_in_holidays(holiday_hash)
  # iterate through holiday_hash and print items such that your readout resembles:
  # Winter:
  #   Christmas: Lights, Wreath
  #   New Years: Party Hats
  # Summer:
  #   Fourth Of July: Fireworks, BBQ
  # etc.
  holiday_hash.each do |season, holiday|
    puts "#{season.to_s.capitalize}:"
    holiday.each do |holiday_name, supplies|
      puts "  #{holiday_name.to_s.split("_").collect {|split_holiday_name| split_holiday_name.capitalize}.join(" ")}: #{supplies.join(", ")}"
    end
  end
end

🙂

I wish I could have done a few more lessons tonight but my next lesson has me watching a 1:10:00 long video. It was still a good productive weekday.

Time spent today: 4:08
Time spent total: 45:15
Lessons completed today: 12
Lessons completed total: 230

Day 10

I got a lot done this morning. Especially after the coffee kicked in. When I started I was definitely not 100% awake. I even did some extra credit in a lab which I typically do not do. I’ve never been the type to want to score 105 on a test because I aced it AND the extra credit. I did this one specifically because I knew it wouldn’t take much time as I was writing a generic method for a specific method I had just completed. Basically inserting placeholders where the original problem had specific items.

So first I solved this:
Advanced: Try building a method swap_elements_from_to that takes three arguments, array, index, destination_index, that will allow you to specify the index of the element you would like to move to a new index.
Like this:

def swap_elements_from_to(array, index, new_index)
  array[index], array[new_index] = array[new_index], array[index]
  return array
end

Then I did this:
Advanced #2: Try writing test coverage for it!
Like this:

# Question 4 Bonus
describe 'swap_elements_from_to' do
  it 'swaps elements and allows you to specify the index of the element you would like to move to a new index' do
    expect(swap_elements_from_to(["one", "two", "three"], 2, 1)).to eq(["one", "three", "two"])
  end
end

And it worked!

I successfully wrote my first test! I was stoked! I know that I’ll have to write my own tests in the future so this small win was nice.

As I kept on working, I must say, the fact that I got this as an error is awesome:

NoMethodError:
undefined method `starts_with?' for "apple":String
Did you mean?  start_with?

I can definitely see why test-driven development (TDD) makes so much sense. I had been thinking it was almost like doing double the work but I could’ve been stuck there forever because of one letter.

Time spent today: 2:39
Time spent total: 41:07
Lessons completed today: 9
Lessons completed total: 218

Day 9

Today was a slow day. The kids didn’t cooperate this morning and I got zero coding done before all three children were awake. That’s fine though. I didn’t expect every day to go perfectly to plan. It was actually a nice refresher day not starting with coding but still getting the rest of my morning routine done.

I did get in my night time session (minus a few minutes for good night kisses). I had some more in-depth labs today and learned a lot about primality algorithms when I had the challenge of coding a test to see if a number is prime without using the Math library.

My code works but definitely is not something that would ever get put into production. It is not efficient, at all.

def prime?(n)
  if n <= 1
    return false
  elsif n <= 3
    return true
  else (2..n/2).none? do |x|
    n % x == 0
  end
  end
end

I could make it even more efficient by changing that n/2 to the square root of n but I couldn’t get that helper method working. In the end, I would want to implement something like what I found online that states:

So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form . This is 3 times as fast as testing all m.

m is what’s happening right now in my solution.

Luckily, I won’t ever have to do that because Math is a library that I’ll be using in real life. I kind of felt like I did in my college Finance class when the professor told us we had to memorize all the formulas even though she had zero memorized herself because her computer did everything for her at her day job.

Time spent today: 1:44
Time spent total: 38:28
Lessons completed today: 3
Lessons completed total: 209

Day 8

Started the day off with a win before 8 am. I was working on a lab to take an array and return a string with Oxford commas inserted. I finished the lab and had it passing tests then refactored and chopped two lines of code out of it. This made me happy.
Original code:

def oxford_comma(array)
  if array.length == 1
    return "#{array[0]}"
  elsif array.length == 2
    return array.join(" and ")
  elsif array.length >= 3
    new_last_array_item = "and #{array[-1]}"
    array.pop
    array.push(new_last_array_item)
    return array.join(", ")
  end
end

Refactored code:

def oxford_comma(array)
  if array.length == 1
    return "#{array[0]}"
  elsif array.length == 2
    return array.join(" and ")
  elsif array.length >= 3
    array[-1] = "and #{array[-1]}"
    return array.join(", ")
  end
end

Another refactoring win today while the kids were napping. I had the following to accomplish: “Write a method called reverse_each_word that takes in a string argument of a sentence and returns that same sentence with each word reversed in place.”

# FIRST METHOD THAT PASSED
def reverse_each_word(array)
  new_array = array.split(" ")
  reversed_array = new_array.each {|x| x.reverse!}
  return reversed_array.join(" ")
end

# FIRST REFACTORED CODE
def reverse_each_word(array)
  new_array = array.split(" ")
  new_array.collect {|x| x.reverse!}
  new_array.join(" ")
end

# FINAL SOLUTION
def reverse_each_word(array)
  array.split(" ").collect {|x| x.reverse!}.join(" ")
end

I realized that I could chain the mehods together since I was calling them all on the same element. Then I also realized that it was going to return automatically from the method so I had no need for the return either. Ruby-Doc is quickly becoming one of my best friends while working on this Procedural Ruby section.

Today was the day of Slack distraction. This can be seen by the number of lessons I completed. However, networking and connecting with other students is important to an extent as well. A few other students and I started down the rabbit hole of health and wellness and biohacking etc… I was able to share some of the things I do like my morning stretching from Gymnastic Bodies, how I drink my coffee cocktail (coffee + grassfed ghee currently), and meditation utilizing Calm. Also how I diffuse essential oils in the office I study/code in and listen to Brain.fm while I code. How I have some Four Sigmatic Lion’s Mane Elixir on the way from Thrive Market (because it’s <$26 there) and some Brain Boost from Amazon.

Time spent today: 3:17
Time spent total: 36:43
Lessons completed today: 9
Lessons completed total: 206