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.  🙁

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.

 

 

spike_and_rarity__s_heart_shaped_fire_ruby_by_edwardtenDid you know it’s possible to run (aka build) your Ruby code straight from inside of Sublime Text 2?

That’s easy, just make sure your code is saved as a .rb file and hit Command+B.  But wait, there’s more…

Did you also know it’s possible to run your Ruby code straight from inside of Sublime Text 2… using RVM’s default Ruby?

There might be a better way but here’s one a quick and dirty for an OSX environment.  If you are using Linux, it’s hopefully the same but with the file paths modified:

Step 1:
Cut a hole in a box.

Step 2:
Open `~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build`
And replace it’s contents with the following (be sure to substitute your user name):

 

Step 3:

  • Launch Sublime Text 2
  • Open a ruby file
  • Hit Command+B
  • The Sublime Text console should pop up with your output. If you want it to go away, hit escape.

Want to prove it’s working?

Here’s a quick script that computes factorial and then prints out the result AND the ruby version using ruby’s constant “RUBY_VERSION”:

 

Drop that into your own factorial.rb, open it in Sublime Text, and hit Command+B.  It should look something like this:

sublime-text-doing-the-most

As you can see above, that’s 1.9.3… and as you can see below… that’s what my RVM is set to use as it’s default ruby.

 

EVIL MASTERMIND... muhahahah aha AppAcademy had us coding some games today! Specifically, Mastermind and Hangman.

When my pair Brittney and I started this we had both never heard of mastermind; so that was the first challenge. Turns out mastermind is pretty straight forward AND it’s actually a really cool game.

Let me explain the rules of mastermind first…

You basically have a board that allows for 10 turns. There’s a code-maker and a code-breaker. The board has 4 columns and a row for each of the 10 turns. Each row (or turn) also includes a “correctness area” (just baked that term up fresh) for the code-maker to indicate how many of the 4 were wrong, near or correct; I’ll explain those soon. For our purposes the computer was the code-maker.

The code-maker’s role is to make a secret “code” that consists of 4 colors selected from a list of 6 available options. Once the code-maker has selected a code he keeps it a secret and allows the code-breaker to guess it.

As I mentioned above, the code-breaker gets 10 attempts to crack the code. Each time he makes a guess he sets 4 colors from the 6 available on the row corresponding to his turn. At that point, the code-maker takes a look at the code-breaker’s choices and using the “correctness area” indicates if any of his/her choices were exact, near or wrong.

If a choice was exact, meaning the color and column selected were correct, a black marker is placed in the “correctness area”.
If a choice was near, meaning the code-breaker selected a correct color but didn’t place it in the correct spot, a grey dot is placed in the “correctness area”.
If a choice is flat in every sense, meaning it’s a color not used in the code, then we do nothing.

If the code-breaker guesses the code before the 10 turns are over, he wins! woot!

Our implementation basically had 3 classes a Computer (the code-maker), a Player (code breaker) and a Game class that actually “played” the game.

The full source is as follows:

 

pairon

Under construction still…

Today was Awesome.  In my past experiences I’ve found that pairs balance out as they work but with Kriti I think we balanced out as a whole.  To explain that a bit further… Kriti is incredibly strong with recursion problems … like straight up a bad ass (and I have a line of code to show for it) and I’m stronger with some of the more common Ruby idioms — test first and similar stuff in that vein.  Instad of balancing out on a per problem basis it felt like we kind of showed our strengths against problems we were better at and the result was a good balance across entire duration of working together.

redneck-recursion

 

We worked on a lot of recursive problems and while working on one in particular ended up making a pretty ridiculous mistake… We used #each instead of #map. After probably an hour of trying to figure out why our ___________ method wouldn’t work we realized that we had been using #each (probably out of habit since it’s so common) and our results were coming out totally unchanged.

We actually ended up writing the same method like 3 different ways trying to resolve the issue and FINALLY zeroed in the fact that nothing was changing -> #each iterates and doesn’t make changes -> #map iterates and DOES make changes -> OMFGWTF

In other news, here’s an awesome, awesome, method that Kriti came up with.

____________________f

giggityHere’s a fun snippet of code we wrote today at AppAcademy.

Basically the goal was to take a bunch of couples who were into swinging and swap all of their partners.  Yep, swinging.. as in:

Swinging or (rarely) partner swapping is a non-monogamous behavior, in which singles or partners in a committed relationship engage in sexual activities with others as a recreational or social activity.[1] Swinging can take place in a number of contexts, ranging from spontaneous sexual activity at informal gatherings of friends to planned regular social meetings to hooking up with like-minded people at a swingers’ club. It can also involve Internet-based swinger social networking services online.
Wikipedia

In order to properly swing, a couple is supposed to show up at a party and swap their partner for another partner.  For our purposes we were to assume couples were heterosexual males and females.

The code we came up with was this:

Basically we found that we could randomize all of the couples then shift their partners by one.  By doing it this way we could ensure things that both things were spicy AND that a man never ended up with the same woman he arrived with.

Ned Ruggeri - AppAcademy teacher

Today marks the first day of my sprint through San Francisco based AppAcademy. It’s a pretty interesting setup.  Basically, you’re paired with a random person everyday and work through a large suite of problems assigned for that particular day.

To say the least, you and your pair bust ass HARD until the end of the day.  The day ends with more work — you are now assigned another pair and must review their code.  After reviewing the other pair’s code and sending them some comments the final part of the day (now night) is doing the readings for the next day’s schedule.

It’s very intense but the amount of code we are producing is really fantastic.

The teachers and TAs are VERY approachable.  Ned Ruggeri (mug shot on the right) presents complex concepts in great detail and definitely seems to genuinely care if people are “getting” things or not.

It’s a great environment.

I recently setup my development system from scratch and one of the tasks is installing PostgreSQL as my apps are almost always deployed to Heroku and they <3 PG.

As you may or may not know, Lion ships with a castrated Postgres installation which (if you don’t already know this (!!!!)) can make it challenging to use something like Homebrew for your installation medium.

To avoid this whole situation I suggest you checkout Postgres.app.  It’s written by the guys @ Heroku and is essentially a .app that you drop in your Applications folder.  Run whenever you want a PG server — it’s THAT easy.

Well… Sorta…

After installing it I had some issues but was able to figure it out so that’s really the “gotcha” part of this post.

On first launch i realized i couldn’t connect to the PG server and immediately went to my system’s console for clues.  This was sitting there looking pretty:

 

Ye olde  “Failed system call was shmget” error….

I believe the root of this issue is that OSX’s shared memory configuration is just really small. And after Googling around I found this post on Railscasts.com by Denis Savitsky (aka sadfuzzy).

In a nut shell there are two ways to fix this:

1- Temporary Solution: Gone after reboot.

2- Permanent Solution: Forever, forever, ever, forever, ever? Yep. After reboot the settings will stick.

 

You can also see this post for further details.

 

Some Preface

An app I’m working on has a rake that that basically gathers data from the web.  Doing things serially is really REALLY slow and to get around this I started using the (fantastic) parallel gem by Michael Grosser.

This gem is great — it’s simple to use and in something like 3 minutes after installing it I had my task running in 10 processes and absolutely murdering the work that needed to be done.

The Issue

I swapped databases not too long ago and moved from MySQL to PostgreSQL for full text search (which i don’t use anymore) and to stay in line with my host, Heroku.

Today i attempted to run the task against my PostgreSQL db and came up with an error I had never seen before:

message type 0x5a arrived from server while idle

WHUT?

After some investigating on Google I found the root cause — PostgreSQL does not allow using the same connection for more than one thread.

That’s pretty straight forward.

I believe the issue was that I had something like 10 ruby processes that were spawned from 1 process that was holding the db connection.  Not allowed!

The Solution

The solution is actually very straight forward as well.  To get around this you simply need to reconnect to the database each time you spawn a process or thread.

What does this look like in code?

Before (bad) :

After (GOOD):

 

And that’s pretty much it.