Git: Amend

ice_cube_gangster_fishing_at_first_i_was_likeHave you ever had this happen to you?

  1. You finish writing some awesome code.  You’re a bad ass and you know it.
  2. You hit the command line, because you stopped creating GUI interfaces using visual basic a long time ago.
  3. Issue a little “git add” here and there, add some broth, a potato.  Baby, you’ve got a stew going… So you commit!
  4. You do the habitual post-commit “git status” for no good reason and there it is, the file you friggen forgot to add to the commit you already committed to.

Fortunately git has a really convenient way to amend to your last commit!  Just simply stage the file(s) you missed and run:

git commit --amend

Hartl’s Description of a Database Index

Today at AppAcademy we have gone through almost all of Michael Hartl’s rails tutorial.  I just wanted to quote Hartl explanation of what a database index is and how it works.  I love the analogy because it’s probably the most simplistic examples I’ve probably ever seen:

Putting an index on the email column fixes the problem. To understand a database index, it’s helpful to consider the analogy of a book index. In a book, to find all the occurrences of a given string, say “foobar”, you would have to scan each page for “foobar”. With a book index, on the other hand, you can just look up “foobar” in the index to see all the pages containing “foobar”. A database index works essentially the same way.


||= WAT?

wat-gigantic-duckFirst off, I stole this pic from Greg Pike’s post about Javascript.  Now that the confession is out of the way…

Today at App Academy my pair and I were asked to explain the meaning of Ruby’s “double pipe equals” operator  to our cohorts.  I figured that might be a good topic to post here.

The double pipe equals, which will be referred to as “||=” from now on, is an operator that’s used for conditional assignment.

In a nutshell the||= operator’s usage is something like this:

blah ||= blabbity_blah()

This essentially says: “if blah is false then set it to blabbity_blah(), otherwise leave it set to whatever it was before we started”

Here’s a contrived, but-still-real-code, example lifted (and modified) from Wikibooks:


['Cogito','ergo','sum'].product(['renè', 'descartes'])

descartes looking flyHere’s a cool chain of info nuggets that starts @ Renè Descartes and ends @ Ruby’s Array.product, lol.

Renè Descartes was a French philosopher and mathematician born in 1596.

He’s known for lots of things but one of the most popular is the following quote  from a passage in his publication Discourse on Method (1637):

I think, therefore I am” (or the corrected version “I am thinking, therefore I exist“)

Renè Descartes  developed “analytic geometry”, an algebraic description of geometry.  A pair of numbers can represent a specific coordinate on a plane or graph.  Sets of coordinates can be represented on a graph as well… and this gets us closer to the end of this nugget chain…

“Analytic geometry” is also known as “Cartesian geometry”… with the name stemming from Descartes’ own name.  In the Cartesian view, when given a plane (think … graph), any point on the plane can be represented by coordinates — in a 2 dimensional plane these might be X and Y, in a 3 dimensional space this might be something like X, Y and Z.

A cartesian product is the result of taking  two sets and returning all permutations of their combined results.  So for example, if you had a set of numbers named X that contained: “1,2,3″ and a set of numbers called Y that contained “-5,-6,-7″, their cartesian product would be:

[1, -5], [1, -6], [1, -7], [2, -5], [2, -6], [2, -7], [3, -5], [3, -6], [3, -7]

And finally getting back to Ruby…. this is where Array.product gets it’s name!

Array.product is awesome and will get you the Cartesian product for a given array against a number of other arrays!


Quick and Dirty: Public vs Private vs Protected

im here to serve and protect and get my treatsRuby provides 3 ways of classifying a methods in a Class: Public, Private and Protected.  These keywords are used to declare a method’s visibility and essentially what you’re doing is restricting access to the individual methods contained in your class.

Here’s a quick and dirty about the differences:


These methods are accessible by anyone who calls your class.  They define your class’s public “interface”.


Protected methods are accessible by objects of the same class.  By this definition, a good use case is when classes extend your class and want to access those methods.  The subclass will retain access into the super class’s protected methods.


Private methods can mask the gory details of how the public/protected take input and generate output.  One way to look at this is that these methods are essentially for the “author of the object/class”.  And by “author” I do mean the guy who wrote the code.  Because private methods are not publicly exposed — aka someone who calls your class doesn’t have access to them — private can be considered free for refactoring, changing and deletion without worrying that your outside callers are going to have problems.

Parallel Assignment

Here’s a cool trick for dealing with situations where you want to assign multiple values at the same time.

The technique used is called parallel assignment and it looks something like this:



If you’re working with game boards, this is super useful way to pass around board coordinates!!

Something to Lookout For

infinite-loopIf anyone actually reads this, here’s an unusual circumstance to look out for… circular object references that cause Pry to go BSC.

(For those who don’t know, Pry is a Ruby shell that can be used as a replacement for the default Ruby shell, IRB (like IPython for python people).)

Today I witnessed a pretty strange phenomenon where I had a game board that contained a bunch of objects representing items on the board.  Each item involved in the game also contacted a reference back to the game board.  After building this circular awareness we starting having problems with Pry where it would just flat out lock up and tax the CPU like crazy.

After some investigation we narrowed things down to the inspect method and the fact that whenever a statement in pry is executed (and irb too… I think) the inspect method is called on it.  This ultimately gives you the nice visual representation of your objects we all love.

After some investigation and soliciting help from peers it looks like this was probably caused by the fact that the game board object referenced other game objects that in turn referenced the game board…. and so on…

In the past, this has NEVER been a problem for me, and later in the day I wasn’t about to reproduce it with different code… the situation today definitely caused a problem and wasted at least 30 minutes.  :(

Merge and Rebase

Screen Shot 2013-01-15 at 7.30.15 AMSome notes on thee specifics of merging vs rebasing with Git.

What is a Merge?
When you merge one branch into another branch the branch being merged into receives a single commit that basically brings it up to the current state of the source branch.

This commit is created automatically by git and essentially represents all of the differences between the branches stuffed into one change. If you were to `git log` in the branch that was merged into, you wouldn’t see any of the commits that had been made in the source branch.

What is a Rebase?
With rebase you basically say “use another branch as new base for my work”.  Behind the scenes what appears to be happening is that git takes the current branch and rewinds any updates you have made on it.  It then moves the starting point to the head of the branch you’re rebasing against.  Finally all of the rewound changes are then applied on TOP of the updated/new starting point.

One thing to keep in mind with rebasing is that it will rewrite commits.  Senko’s blog post explains it pretty poignantly:

In rebase, you change your branch (that’s being rebased) so it looks like it was branched off a new base, not the original one. This involves rewriting the commits, so you’ll end up with different commit IDs.