John Keith

Ruby. Rails. Launch Academy.

Demo of Theme

Hi! This is a demo of an Octopress theme. If you want to read the real blog that the posts come from, head over to Enjoy!

Working With JSON in Ruby and jQuery

JSON is one of the major data formats out there on the web, known for its readability. Now, I was a little unsure about that readability aspect after pulling up my first JSON document and seeing this mess.

Crazy JSON!
The first step towards wrapping your head around JSON is to see it printed in a prettier fashion. Head over to JSON Online Editor and using the “Open URL” option you can see that JSON data is actually much more presentable than it lets on. (If you need a JSON example to play with while reading this, don’t hesitate to use my Treehouse profile, as this provides you with a complex, multidimensional set of data to manipulate).

I decided to start by figuring out how to work with JSON in Ruby, as currently I certainly feel more comfortable with Ruby than Javascript. Luckily, Ruby makes handling JSON from the web easy with a few of its built in libraries and modules.

Spaced Repetition and the Novice Programmer

Nothing compares with that moment when an idea hits you over the head like a ton of bricks labeled “Duh!” I experienced one of those today after coming across this great Medium piece by Mattan Griffel about using spaced repetition for improving recall, then following that to an article by Derek Sivers about studying programming languages using the same technique. At the end of Sivers’ article (and after reading that this idea has been around for over a century!), I was left wondering why spaced repetition is not a foundational part of any and all learning we do.

It is hardly a complex concept: spaced repetition is the reviewing of learned information at specifically defined intervals, with those intervals being determined by your comfort with the information. You commonly run across this technique in flashcard apps - the Brainscape suite of apps is a good example, along with the more DIY example of Anki that Griffel explores. The difference from the 3 x 5 colored notecards you made in middle school is that Anki and Brainscape are both powered by algorithms that decide when you should view each flashcard. This allows for a much more focused reviewing - the cards with basic information only show up sporadically, while the cards containing more involved ideas or information that you find yourself struggling with appear more frequently.

Brainscape web version (The colored rating buttons at the bottom of the image above are part of the mechanism for determining when you will see certain cards in Brainscape. The higher you rate how well you knew the information, the less you will see the card.)

What baffles me is that spaced repetition is not built into every online learning platform. Imagine working on a Treehouse track or a Codeacademy course and being intermittently offered opportunities to quickly review the information you have been absorbing from other lessons. For the novice programmer, this repetition would be invaluable, as so often you learn about a new function (Ruby’s inject always comes to mind for me) or quirk of the language and then do not see it again for a long stretch of time. The huge amount of information to absorb and explore in the early stages of learning a language makes deliberate and meaningful repetition of what you’ve covered essential.

What also surprises me is that we do not see spaced repetition more in offline curriculum. I cannot imagine how much better my long term understanding and ability to use what I learned in insert any high school subject here would be had we been taught to use spaced repetition tools. It seems our whole education system is focused on the forward - the next chapter, the next test, the next year - and is a system that does not reward the concentrated study that leads to mastery. You have to wonder too if part of the motive for this forward inertia in our educational structure is profit: how many SAT cram courses/books/apps are you going to sell if students actually learn the material they need to know the first time they are taught it?

That said, I’m going to dive into the Anki app and see what I can create as far as a flashcards repository for learning Ruby and Rails. Depending on how the app works, I’ll try to post what I develop here for download.

Trying to Remember the Small Stuff

Refactoring is really fun, especially in those moments when you have an “Aha!” flash and one of those little details clicks into place. I had that today with method I wrote for changing the sorting on a table in a Rails view.

def sortable(column, title = nil)
  title ||= column.titleize
  direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
  font_awesome_direction = direction == "asc" ? "up" : "down"
  css_class = column == sort_column ? "fa-caret-#{font_awesome_direction}" : nil
  link_to title, {:sort => column, :direction => direction}, {class: css_class}

That sortable method was placed in my application_helper.rb. It works with these two methods from my view controller.

def sort_direction
  %w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"

def sort_column
  Expense.column_names.include?(params[:sort]) ? params[:sort] : "date"

And all of these are put into place with the index method in that same view controller.

def index
  @expenses = Expense.order(sort_column + ' ' + sort_direction).paginate(page: params[:page])

Initially, what I’d written wasn’t terrible: it worked in the browser, my columns resorted themselves on prompting, and a nice little caret was provided as feedback and a guide post for the user. That said, I was irked by the way the caret was smushed right up against the text and the way that adding the caret threw off the font sizing and styling I had going at the top of my table.

So, I took a step back and tried to determine out if it was possible to embed HTML inside of a link_to in Rails. A couple minutes later, I had figured out it was perfectly acceptable and learned a little bit about the nifty #html_safe method that Rails provides. This led me to a rewritten last line that I think provides a much more pleasing output.

link_to "<i class='fa #{css_class}'></i> ".html_safe+title, {:sort => column, :direction => direction}

It was also great to have one of those moments when all that reading comes in handy. I couldn’t get the icons to appear the first time I fired up the page, because I had wrapped my icon HTML in single quotes instead of double. Using double quotes on the outside and then single on the inside allows Ruby to interpolate the css_class variable. Nothing fancy, but still a good learning experience that will hopefully keep me more consistent with my use of quotes.

Why Launch Academy in a Nutshell

As another part of our pre-work for Launch Academy, we’ve been asked to setup a blog. Luckily, I actually (miraculously) got this Octopress installation started back when I was applying to Launch. That said, I liked one of the writing prompts provided to us as part of this exercise - “Three Reasons I Felt Launch Academy Was the Best Fit for Me” - and I thought offering my three cents on why I’m heading to Launch could be informative for anyone else looking to apply to development bootcamps.

Student-centric approach to everything

Above all else, Launch Academy feels very student-centered. From the moment you start looking at their webpage to your first conversations with their experience engineers, the immediate impression you receive is that Launch is completely dedicated to their mission of transforming students from all walks of life into software engineers. Now, I’m sure many, many of the other bootcamps out there are equally fixated on this goal, but I felt that Launch Academy had the greatest commitment to their students of any of the programs I talked with.

Excellent, comprehensive curriculum

One of the parts of Launch Academy that I spent the most time dissecting before making my commitment to attend was the curriculum. I wanted to make sure that after Launch I would feel prepared to jump into the job market as a web developer. I can say from many conversations and emails with the Launch staff that I feel confident the program will provide the broad exposure and focus on fundamental skills to transform us into capable and confident web developers.

Post-graduation assistance

This may be part and parcel of Launch’s commitment to their students, but the career assistance that Launch offers to its graduates was another huge reason that I decided on them over other bootcamps. While many other programs talk about their tuition breaks for taking a job through a partner company, I believe Launch is much more focused on helping you find a job where you will both succeed and be happy, rather than one that benefits their partnerships.

And of course, Boston! Having come back from Korea almost three years ago, I’m ready for a new adventure and a radical change of scenery. While I will miss Columbia (weather-wise and otherwise), I cannot wait to come up to Boston in May.

Dynamically Control Button Labels With a Helper Method

Reader beware: I’m learning Rails right now. In no way, shape or form should this post or any posts here for at least the next month and a half be taken as gospel truth of how to do things right in Rails. They probably are way, way out in right field.

Okay, say you have a view and in that view you have a beautiful button. If you want to assign a name to that button dynamically, say based on the truthiness of some variable, here’s an easy way to do it.

Open up the _helper.rb file for your model. In your _helper file, write a method that will provide you two pieces of text, depending on a certain condition.

def sign_up_btn
  if User.sign_up_allowed == true
    "Prevent new account creation"
    "Allow new account creation"

Or, if you want to be fancy, try this refactored-while-in-the-midst-of-blogging-version.

def sign_up_btn
  User.sign_up_allowed ? "Prevent new account creation" : "Allow new account creation"

Then, back over in your view file, add this bit to create the button and assign it a title based on the results of the method above.

<%= button_to action: "disallow_sign_up" do %><%= "#{sign_up_btn}" %><% end %>

(Just so you get a fuller picture, here’s my disallow_sign_up method from my users_controller. I wrote it as a way for an admin to turn off access to the sign up page in the expense tracking app I’m working on.)

def disallow_sign_up
  User.sign_up_allowed ? User.sign_up_allowed = false : User.sign_up_allowed = true
  redirect_to users_index_path

I have a serious fixation on the ternary operator, if you haven’t noticed.