Nicolás Hock Isaza

Ruby, Rails, Programming and Random Thoughts

Tower of Hanoi With a Stack

I've recently subscribed to Coding for Interviews and received my first email today. The content is great and you should join. Even if you're not looking for a job, it's a short read with nice topics to refresh your memory.

Today's assignment was with stacks. I've used stacks to reverse a list in O(n) but today's challenge was to solve the Tower of Hanoi problem without recursion.

Here's my solution:

There's another iterative solution but I haven't implemented it. So, if you did or have some other way to solve this, I'd love to know on Twitter @nhocki

Stop Using Config Files for Secret Data

Seriously, it's your fault that Github exposed your keys. If you want to add everything to your repo (not that this is a good idea though), at least make it private.

If you want to have per-environment variables, use the environment variables. But my development env would be full of crap! some of you may cry… well, there's a simple solution for that. Use Dotenv.

With Dotenv you only need to add a .env file to your application root directory and you're done. Here's a quick example:

# .env

Then in your code, simply use ENV[] and be happy (and safe, but mostly happy)

# some config file
config.fog_directory  = ENV['S3_BUCKET']

And just like that, you can setup your server's config environments and not worry about supposed-to-be-private public information.

One really cool thing about this is that this variables will only be loaded in that specific application. It won't change your environment. So when you stop the servers, your environment won't have the .env variables.

You can read more about Dotenv here.

Easily Add Versions to Your Gemfile

Since bundler came out, managing dependencies on ruby applications has been amazing. Combined with some bootstrap script, it's really easy to download some source code, install, and run an application.

There is still some room for improvement though. I've always supported the use of SemVer and pessimistic locking (the ~>). This will allow you to update applications without pain. And, with all this rails vulnerabilities, you need just that.

But, since people don't always have the versions on their Gemfile, this can get a little messy. So, I've created a simple gem to help you with that.

Welcome version_gemfile

  • 1. Simply install it: $ gem install version_gemfile
  • 2. Go to your rails application: $ cd ~/my/ruby/application
  • 3. And add the versions to your Gemfile: $ version_gemfile

How does it work?

This gem will go through each line in your Gemfile looking for dependencies that have no versions. Then, will look into your Gemfile.lock for the version you are currently using and add a pessimistic lock on that version.

The code can be found on Github. If you have any problems, open an issue there or ping me on Twitter.

Reverse a List in O(n)

For about a year, I've always had a question I ask someone I'm interviewing for a programming position. I've asked this around 3 times and got some nice answers, but I had never implemented it. The question is really simple:

Given a linked list, how would you reverse it in O(n)?

Here's my solution:

How would you do it? There's a really simple solution using a stack, but if you find another one, I'd love to hear about it on Twitter @nhocki

Using Liquid `include` With DB Templates

I am working on a new application that will use some Liquid templates to allow our clients to edit some templates.

We will store these templates in the Database, load them, parse them and render them. So far, no magic.

But, I needed to have some "partials" for these. I wanted some common elements (like image galleries) to be available for the client to use in that specific template.

The way you normally accomplish this is by using an include tag, but I needed a little bit more control. Not all the clients will have the same partials and I also wanted the clients to be able to edit these partials.

So after reading the Liquid source code, I found a really easy way to do this.

You just need an object that responds to read_template_file and it can take 1 or 2 arguments (please, go for 2). This method must return the Liquid source of the partial (not a parsed template).

So, here's a really simple one that will work:

Default FileSystem

You could also set the default FileSystem with Liquid::Template.file_system = This may be useful if you want the partials to be shared on the database but not different for every user.

Any other way?

Even though I find this to be really easy, another way may exist. If you know of a better way to do this, please let me know.

Seriously Just Do It

I have finished my first "working season" at MIT. I got here on October 4. It's been an amazing and weird journey.

But I have to admit that at first I was a bad employee. I was really used to have somebody telling me what to do all the time. When I arrived at MIT that didn't happen. At all. I was given some "long term" tasks and that was it. I just had to do them.

Since no one was all over me checking my progress or asking me how I was doing on everything, I was waisting a lot (*most*) of my time. ESPNDeportes got a lot of visits from me. I started using Facebook on regular basis. Threadless didn't have enough T-shirts for me. Twitter was not "real time enough"…

But, with time, I started working and I can't say how grateful I am for this way of managing. I've learned to be much more productive on my own. To manage my time. To be much more responsible. But, I guess the best thing about this was that I learned that you just need to shut up, close all the meaningless things you have open and get shit done.

Sure, if you need help, ask. If what you're doing is wrong, try again. But you won't know something doesn't work if it's just an idea.

I became much more productive in everything. Not only MIT work stuff. Personal (disposable) things I wanted to try. Things I wanted to learn. Things I wanted to read (even my Instapaper queue went down. By a lot!).

So, just try it out. It may take more than a week to get used to it, but just try it. Close this post and every other distracting thing and do something.

Use the Community for Something More

For a while, this idea has been wondering around in my head.

I would love to have some web-development community in Medellín, but I would like it to be "something more". I love the idea of learning from others, teaching and getting people involved. I know there are more people that are looking forward for this.

Sadly, we don't have a community like the one in Boston. It's amazing how many events there are here. Boston.rb meets up to 3 times a month! It's just crazy! You just need to go to Meetup to find an event of your interest! All filled with people eager to share and help!

But I would love to put all that lovin' to some "charity" work. Get real projects for people that need it. It doesn't have to be 100% free, but not charging nearly as much as you would normally do it!

For example, I know a Volunteer Fire Fighter Department that needs to build a simple learning system to distribute knowledge because ignorance is taking a lot of lives (from firefighters and people they're trying to save). Hell, they also need a new website!

I also know about a guy that needs a website for an organisation that helps small kids with cancer in Africa.

All these projects could use the expertise of "old" developers and enthusiasm from the ones that are just learning. I've found that working on a real project is way better than learning on your backyard. So why not take something that could use more man-power and pair program on these websites?

I'm not saying we should not have presentations! Presentations should still be the center of this. Sharing new stuff is the most important part for the community. But maybe having some hackathons where we work on these projects.

So, what do you think? Would something like this work? Would people be interested? Those organizations could, for example, sponsor the meetings giving some beer and food! Developing companies could also sponsor this and get some mention on the created websites. I know we would do it!

I can't do it right now. I'm 2.5K miles away! And I don't think it's an easy thing to organize, but I would like to know if people is actually interested before I throw myself sending emails and talking to people, so please contact me if you are, either by leaving a comment or pinging me on Twitter(@nhocki).

If you liked this idea, you might one to up vote it in Hackernews.

Fix Xcode Path

I updated my Xcode version this week and to my surprise they moved it from /Developer to /Applications/Xcode, the problem is that now all the developer tools went missing. Here's the easy solution for this:

sudo /usr/bin/xcode-select -switch /Applications/

That's it. Just let Xcode know where to look for the developer tools again.

Mixing Presenters and Helpers

I just finished The Rails View and the best chapter for me was about presenters. I've been working with this for a while and I have to admit I love the approach presented on the view.

All the presenters I've done were instantiated in the controller. In the book they suggest that if the presenter doesn't depend on params or session variables it's the view responsibility to create it. This is extremely flexible specially when we're presenting various objects on the same view. Here's an example to present users in our application, let's use the same presenter for the index and show actions, but let's make it in a really simple way!

The Helper & View

Let's create the helper method first. It's really simple and will take a block that we can use to build a custom markup "on the fly".

Now, let's take a look at the views. In the show action, we're going to present only one user, and we can easily build the custom view for this. In the index action, we'll present all a "basic info" for the each user. We have that in a partial because this is used for all the collections of users in our application. Here's the view code:

The Presenter, A.K.A What's happening here?

What makes this really clean is that we can actually use <%= %> directly on our helper method. This works because we'll define the to_s method in our presenter! In this method we render our partial in the context of the helper, which is amazing. Here's the code for that:

Is that simple! We have a unique interface (the helper) that will work great in both places! We could obviously make the presenter more complex, otherwise, why use it?

Even though I haven't used this approach a lot, I'm really happy with it. I felt the difference instantly and it made a lot of sense to me. Being able to mix this with a flexible layout is really powerful!


I'm also really interested in how people are using presenters. I'd love to gain more feedbacks and points of view about this. Feel free to post a comment (here on in the gist) or ping me on Twitter, @nhocki.

Flexible and Friendly Layouts on Rails

I've been working on a "new" project (I'm new to the project, the project itself is 'old') and at the beginning I had to edit a bunch of stuff in the views.

A friend needed to change the header of the page on one action of the UsersController. He is new to rails and was struggling with this, so he asked me for help.

It's pretty common to have a header partial rendered on the view. If you're a lovely developer you would have render "header" and not something like render "layouts/header". The reason behind this is simple. Template Inheritance. This means that if you're working on the UsersController, when rails looks for the header partial, it will first look for users/header moving up the parents chain, ending in application/header. So, next time, put all your layout stuff in application/_partial so you can easily override it!

But this is a controller wide solution, since all the actions from that controller will render the same partial. So, how do you achieve this on a single action? My solution is a 3 line helper:

Now all you have to do is fill your layouts with "content boxes" that can be changed on an controller or action-specific need, giving great flexibility.

How would you get around this?