Archive | July 2014

Life Hacks

While this post is not about programming, ruby, rails, or any of that other stuff, I couldn’t resist sharing this life hack.

I’ve been wanting an Apple TV for a while now because my girlfriend and I use an HDMI cable to watch Netflix and such on our TV. This requires the computer to be sitting near the TV, so very far away from the couch. Now, this isn’t often a problem but when we want to pause whatever we’re watching, we have to walk ALL THE WAY OVER TO THE TV!! This is absolutely unacceptable! (I know, first world problems). Hence the desire for an Apple TV which would allow us to wirelessly display our computer screen on the TV but my fiscally responsible girlfriend thinks this is an unnecessary expense.

Well, given that I already have a bluetooth keyboard for my ipad, I suddenly had the idea to simply connect the keyboard to my laptop so I could press the space bar from the couch to pause the video! WOO HOO!! Problem solved without having to buy an Apple TV!

It gets better. What about a mouse? Well, I could buy a wireless mouse but I don’t have one. So, my genius girlfriend said “There’s got to be an app for that!” After some investigation, I discovered Rowmote. The pro version costs $5 and gives you a trackpad on your ipad/iphone to control the mouse on your mac over a wifi network. I downloaded it, downloaded the Rowmote helper app on my macbook (
and voilĂ , I now have complete remote control of my laptop, projected on the TV, while lazily sitting on the couch, and I set it up while only having to get off the couch twice (to connect the bluetooth keyboard, and to download the Rowmote helper).

This is now my default work-at-home setup, with my keyboard on my lap and my iphone on my side (like a mouse). Added bonus: my posture is much better because I’m looking up at the TV, not down at a screen.

The Rowmote trackpad is surprisingly responsive and fast considering it connects over wifi. In addition to being great for working from home and pausing Netflix from the couch, we’ve discovered that we can easily play 2 player enganglement with our own idevices.


Getting Started with Rails

So after quite a bit of practice and learning ruby, we are finally writing Rails apps! WooHoo! I’m going to walk through a few of the steps to get a rails app up and running on your local machine.
Firstly, you need ruby and rails installed on your computer.
check this with
ruby -v
rails -v

These should tell you the current version of ruby and rails you have.

If you’re using rvm (ruby version manager), there are some extra steps that I’ll omit here.

So here’s how you create a rails app called ‘blog’:

rails new blog

I usually run this with a couple options:

rails new blog -TB

The -T options skips the built-in test unit, which I skip because I use rspec instead. The -B skips the bundle because I usually want to add a couple gems to the Gemfile before I bundle.

Anyway, you should now have a directory called ‘blog’, which you can cd into.

The models, views and controllers are all in the app/ directory.
There are several ways to generate these files.

If you want a quick and easy to set up all three, along with a database to tie to your model, you can run:

rails g(enerate) article author:string title:string body:text

The :string is optional, as string is the default.

This command will create a database migration for the object Article, which has the following properties: an author, and title, and a body. Rails creates all sorts of files for you in the models, views, and controllers folders. Before you start going into those files, you’ll want to run the migration with

rake db:migrate

You can also see what routes rails created for you with

rake routes

From here, you can start modifying your views, controllers and models to customize your app.

There’s obviously much more that I didn’t cover here in getting started with rails, but there are tons of resources out there. I used this one

Getting Started with Rails

Have fun on the Rails!

TDD part 2

Its long overdue, but I wanted to share what I learned about the Red, Green, Refactor process of Test Driven Development, and why it makes sense.

The fist step is to write a failing test. This will be before you have written any code in your program/website/whatever. So since you have no code, of course your first test will be failing.

Then, write the minimum amount of code to get the test passing (green). Once you accomplish that, its time to refactor. You can either refactor the code (to make it more efficient, better, cleaner), or you can refactor your tests (make them more comprehensive, add new one), but don’t refactor your code and your test at the same time.

Once you’re factored, you should have have another failing (red) test, so you can start the process over.

Thats red, green, refactor in a nutshell.

Test Driven Development (TDD)

Now that I actually have some tools to practice TDD, I find it much easier to write and debug a program!

When I was teaching myself javascript and ruby, I would write programs, and in the process, I would frequently want to check if it was working up to a certain point. My solution at the time (before I knew anything about TDD), would involve adding a couple lines of code to print the values of variables to the console, so I could see if they were what I expected. For example, I wrote a program to determine if a number is prime or not, called primes.rb, with a method called isPrime?, and frequently wanted to test some base cases, so I would insert something like:

puts isPrime?(1)
puts isPrime?(2)
puts isPrime?(3)
puts isPrime?(4)
puts isPrime?(42000)
puts isPrime?(97379)

into my code. I would then run the script and hope for:

This was quite a pain because every time I changed the program, I would have to run it again to see if it still worked (assuming it was working before).
In a sense, I was doing test driven development, but in a very crude and amateur way.

So what is Test Driven Development for real?
Using rspec and guard-rspec, which are ruby gems, you can write your own test to check you code as you write it. Using the example above, I could write a primes_spec.rb file (by convention, spec files are placed in a spec/lib/ directory, and should match the lib/ directory at your project root)

If you are running guard in your terminal (I’ll assume you know how to install gems, and figure out how to run guard), whenever you make a change to your primes.rb code, guard will automatically run those tests and give you a pass or fail status.

Now, there is more to TDD than just writing code and writing tests for it. There is a methodology to the process, called ‘Red, Green, Refactor’ which I will discuss in the next blog post.