Archive | Programming RSS for this section

Time Zone issues on Heroku

I’ve run into this problem before and want to document the simple way that I finally fixed it.

I have Van model with a column called ‘departure_time’ in a Rails app this is a DateTime object. When a user/admin edits a van, they have a series of dropdowns to select the datetime using the datetime_select helper in Rails. Everything seems to be fine until I push to heroku, and then whenever I create a new van, or edit one, time time that I choose gets changed by 6 hours. I spend hour trying to figure this out on another app, and finally came across this simple solution:

I was using the bootstrap3-datetimepicker gem, which formats the datetime differently than what needs to go in the database, so I had to do some logic to parse that string into an acceptable datetime object, and then create a DateTime object with or Time.local.

Well, all I need to do to fixe the error is use instead of Time.local. It now works locally and on Heroku.


Algorhythms LLC

I’ve recently started an LLC for my freelance Web Development business. Check it out at

If you know anyone that needs a website or web app, send them my way!

Google Maps in Tabs

I was working on embedding 2 google maps on 2 tabs on a page, and came across a common problem. I want to document my solution, as many of the solutions I came across did not work for me.

I had a page with a tab for a US map, and a tab with an international map.

The problem was that When I clicked on the second tab (the international map), most of the map was grayed out, like this:
Screen Shot 2014-10-08 at 3.04.11 PM

I’d seen this problem on a few stackoverflow pages and blogs, with many suggested solutions. After trying several of them to no success, I made with work with simple solution.

Here is my original code (that didnt’ work):
The html with Bootstrap tabs:

And here is the Javascript using a JQuery google map plugin to do some cool clustering effects on the maps (details edited out and comments inserted):

The problem is that when you click on a new tab, the map object resizes for some reason, and does not resize correctly. The idea to fix this is to resize the map again when the tab is clicked, using JavaScript or some other event listener or something. Well, that didn’t work for me. What did work was to load the entire international map only when I clicked the tab, rather than using JQuery’s $(document).ready.

all I had to change was line 13 in the Javascript above from
var loadInternationalMap = function() {

And then call that function when the tab was clicked (line 3 from the html above):

<a href="#international" role="tab" id="international-tab" onclick="loadInternationalMap();">International</a>

And viola, when I clicked on the international tab, the map loaded correctly.

I’m sure that this problem can be solved in many different ways depending on your particular situation, but the idea to to load (or resize, or re-initialize) the map once the tab is clicked, not just when the page is loaded.

The Perfect Shuffle

In class last week, we created a model of a deck of cards in ruby. I’m sure this has been done many times for all the online card games out there, but I wanted to play around with the functionality of the deck of cards, specifically, I wanted to create a perfect shuffle method.

A perfect shuffle (technically, a faro out-shuffle), is where you split the deck of cards exactly in half (26 cards in each stack), and then perfectly alternate cards when you shuffle. This leaves the top and bottom cards in place. (A faro in-shuffle is when the top card moves to second-from-top, and the bottom card moves to second-from-bottom).

This is an old (and very difficult) slight of hand, and it is useful to magicians because if you can do 8 faro out-shuffles, the deck returns to its original order.

So how did I implement this in ruby? Well, I have a card class that looks like this:

And a deck class that looks like this:

Notice that I have a shuffle method for a random shuffle, which is programmatically interesting in itself, and I’m curious if there is a cleaner way to do this. Here is the logic of my random shuffle method:

  1. Create an empty array to hold the new shuffled deck.
  2. Pick the first card of the current deck.
    1. pick a random number from 1 to 52 (well…okay, 0 to 51)
    2. if that slot in the shuffled_cards array is empty, insert the current card into that slot.
    3. if that slot is already occupied, pick another random number and try again.
    4. repeat steps a-c until an empty slot is found where I can insert that card.
  3. Repeat step 2 for each of the 52 cards.

This method is rather inefficient. For example, once I have 51 cards placed into the new shuffled deck, only one card remains, and there is only one slot for it go. It should take almost no computation at all to just insert the last card in the remaining empty slot. However, the computer will keep generating random numbers from 0..51 until it finds that empty slot, then it will insert the card. Theoretically, that number could never be picked, and it would be in an infinite loop. And even when it does pick the right slot, there is still a lot more computation going on than necessary.

What I need is an efficient algorithm to generate a random permutation of the numbers 0..51, which I can then use to just insert my current cards into the new deck in that random order. Any thoughts or idea or links or code snippets are welcome!

The Perfect Shuffle.

Let’s think about what a perfect shuffle is. Lets say my deck starts out sorted (where the suit order is spades, diamonds, clubs, hearts). Number the cards from 0 to 51 s.t.*
A♠ => 0
2♠ => 1

Q♥ => 50
K♥ => 51

A perfect shuffle will take the top half (cards 0..25) and the bottom half (cards 26..51) and alternate them in the following order:


So the new order of card will be:




Here is my implementation of the faro shuffle:

It may not be the cleanest method, but it works. I do wonder if there is a way to do two simultaneous loops, instead of declaring b as a separate variable, then looping through values of a. Something like the python implementation on the wikipedia page linked to above:

Is there something like this in ruby? maybe

* s.t. is a mathematical abbreviation for such that

First Website!

Here’s my first live website! I built this for my brother and his flooring business in New Castle, CO.

Justin Floors Inc

You can find the repo here on github if you want to take a look.

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.


I’m new to agile development, so I’m going to try to explain what I understand.

Agile development is a methodology for breaking down big projects into smaller, manageable tasks, or stories. These tasks are prioritized by the client, and estimated by the developers. In a short period of time, sometimes called a sprint or iteration (1-3 weeks), a working project is expected. If something doesn’t work, it’s not done. This means that for the first version, or maybe the first sprint, it’s okay for some features to be simplistic, as long as they work. They can be enhanced later, but functionality is obviously more important than bells and whistles.

Of course, things may not always go as planned. With Agile, the development team is expected to adapt to changing requirements, learn whatever is not know to get the job done. These changes may not be part of the original plan. My guess is that this idea of adapting to changes is why this methodology is called “Agile,” but that’s just my guess.