Ruby On The Beach: Week 2

January 18th, 2015 - Ubud, Bali

Just two weeks in to the Ruby On The Beach bootcamp experience, it's incredible to look back at the software concepts I was struggling with ten days ago and laugh at my own naiveté. Although my goal of becoming a bona fide programmer is undoubtedly still far away, it feels great to make progress every day and I've undoubtedly traversed a massive amount of material already.

In an effort to share insights into this experimental learning process, I've decided to share the outcomes from two ways in which I'm quantifying my progress and holding myself accountable throughout the course. The first, RescueTime, is an application that automatically tracks and categorizes how you're spending the time on your computer, providing insights into productivity and the biggest drains on your time. My first Weekly Dashboard shows a pretty focused week overall, with plenty of down time for reading Grantland, catching up on The Challenge, and even streaming NFL playoff games:

Screen Shot 2015-01-18 at 10.10.56 AM

 

The second, Coach.me, is the new name of Lift.do, an app I've written about before. Coach.me now offers chat-based coaching for $14.99/week, but the feature I love is completely free: daily habit check-ins. After experimenting with tracking a variety of different habits over the course of the last year, I've settled on six that are truly fundamental to my personal development and well-being. Most are self-explanatory, but #500WED just means writing 500 Words Every Day about anything that's on your mind. My first week of tracking got off to a solid start:

 

Screen Shot 2015-01-18 at 10.06.22 AMWondering why flossing made the cut? Want to learn more about developing positive changes in your lifestyle and be "scared straight" into developing a flossing habit? Check out this awesome illustrated Medium post.

During the first week, I was extremely motivated and actually really enjoyed diving into the fundamental aspects of programming. The concepts we approached in the second week of the curriculum were more challenging, and I spent significantly more time planted in front of my computer this week (13.6 more hours, according to RescueTime). Although there were stretches where I felt like I was repeatedly banging my head against a brick wall, in retrospect I definitely took huge strides in my understanding of Ruby.

Screen Shot 2015-01-18 at 10.11.15 AM

Although by sheer volume this was the most productive week I've ever had on RescueTime, those late nights of coding took a tool on my energy level and willpower, and I didn't prioritize well enough to have a great week of daily habits.

Screen Shot 2015-01-18 at 10.09.58 AMO Christmas Tree!

One of the most challenging projects we encountered in the curriculum this week was on the surface, incredibly simple: write a program that draws a Christmas Tree that looks like this:

Screen Shot 2015-01-18 at 11.44.18 AM

Appearances can be deceiving. Constructing this program left me utterly stumped for hours at a time, but I was eventually able to grasp the concepts associated with it and will now try to distill the code into terms anyone can understand.

First, notice that the entire program consists of merely printing spaces and asterisks (let's call them stars). In the first row, there's 1 star, followed by 3, then 5, and so on. If we were to represent this sequence as an equation, it could be (row number[starting at 0] = row number * 2 + 1). We can create a chunk of code to represent that:

def calculate_stars row
  row * 2 + 1
end

Ok, WTF is going on here?! First, we're defining a "method" called calculate_stars. Every time we type calculate_stars in different parts of the program, it will run this block of code. Calculate_stars also has an "argument", called row. Now when you run "calculate_stars 7", the method will take 7, multiply it by 2, then add 1. In row 7, calculate_stars knows to create 15 stars!

Second, let's calculate the number of spaces in each row much the same way.

def calculate_spaces width, stars
  width - stars
end

When we run the program at the end, we'll be providing it with two inputs: the number of rows and the width. Think about each individual line: if you know the number of stars and the total width, then you can figure out the number of spaces. Just do width - spaces! That's what is going on here.

Third, let's create another chunk of code that takes these calculations from above and translates it into actual printed stars and spaces. In order to do this, we'll need to run a loop that looks like this. Comments start with #HASHTAGS :)

# start with a blank line 
line = ""
# Below, stars is a variable. 
# It says, if there are 15 stars, run through this 15 times
stars.times do 
# Every time we do this, add a star to that blank line
  line = line + "*"
end 
# Print that line out! 
 puts line

This is a good start, but we need to add in our spaces first. The final result is a method named print line that runs through two variables, the number of spaces and the number of stars. Good thing we calculated those using the first two methods!

def print_line spaces, stars
  line = ""
  spaces.times do
    line = line + " "
  end
  stars.times do
    line = line + "*"
  end
  puts line
end

Finally, we have to put it all together! This time we'll write a master method that uses the three sections we've already built.

# Let's draw_trees with 2 variables: rows and width. 
# This enables the creation of custom size trees. 
def draw_tree rows, width
# Let's run the code below for however many rows there are. 
# |row| will go up by 1 each time through the loop.
  rows.times do |row|
# Remember calculate_stars? Let's use it right now! 
    stars = calculate_stars row
# width - stars is equal to the number of spaces. 
    spaces = calculate_spaces width, stars
    print_line spaces, stars
# This handy line adds a star to both the left and right side. 
    width = width + 1
# End the loop 
  end 
# End the method
end

Now let's check out what happens when we run the program in our terminal with a width of 15 and 10 rows.

Screen Shot 2015-01-18 at 11.43.54 AM Success!! The best part is this program is flexible enough to create any size tree. The final output looks something like this. Can you figure out what each line means?

Screen Shot 2015-01-18 at 11.52.07 AM

Although we eventually figured out how to draw a base, invert the tree, and re-factor our code to include other fancy concepts like arrays and hashes, I think I should leave it at that for now. If you have any questions or certain parts didn't make sense, I would love to hear about it in the comments below!

If you're interested in other perspectives on the "bootcamp in Bali" experience, be sure to check out the blogs of two of my fellow students at http://www.chic-ceo.com/blog and http://rubybali.tumblr.com/.