Super cool way to create Ruby Hash

Today I came across a really cool method in Ruby’s Hash class. I cannot believe I missed it all these days. Its a class method and its syntax is:


Since Ruby is cool, when you do Hash[1,2,3,4] you actually are doing Hash[](1,2,3,4).

So what does it do? It simply creates hashes from Arrays. There are many ways to use this method to create Hashes. We’ll see one by one. The first way is to simply pass an array of elements like this:


When you do that you get the following Hash as the result:

{1=>2, 3=>4}

Neat. Right? So the important point we must note here is the fact that we should always pass in an array with even length else Ruby will raise odd number of arguments for Hash error and you probably understand why this is the case.

The second way is to pass an array of key-value pairs and you will get a hash as the result like so:

Hash[1=>2,3=>4] #=> {1=>2,3=>4}

And the third is especially awesome. Take a look for yourself.

Hash[ [ [1,2], [3,4] ] ] #=> {1=>2,3=>4}

This is very useful when you have an ActiveRecord::Relation object and you want to construct a Hash from the records represented by that object. For example, I have a model called ReferenceDatum. I have three attributes – ref_category, ref_name, ref_value. What if I want to create a hash like {ref_name=>ref_value}? I could just do something like:

Hash[ReferenceDatum.where(:ref_category=>'mycat').map{|r| [r.ref_name,r.ref_value] }]

There are other ways to do this job as well. But I find this pleasing to the eye (barring the part where we do the mapping ofc!).

Have fun!

Amazing support from Heroku

You must have heard of and used Heroku. They are an amazing cloud hosting company. Their success was and is largely because on the ability to deploy with minimal complexity without much knowledge of setting up a production server. This was a boon to beginner developers who wanted to show case and demo their work. I was one such developer who used Heroku frequently. In September this year I deployed a slightly modified build of Diaspora on Heroku and I scaled it up to use 2 web dynos and a worker dyno. I was playing around with it and I knew that if I scale it down after few hours I wouldn’t incur costs because of the free credit that Heroku offers. But unfortunately I forgot to scale it down and I left it just like that.

Last week I received the credit card bill and I was shocked to see that I was charged $70 (~ Rs. 4300) by Heroku. I was baffled and then I realized that I had scaled up my Diaspora instance and wondered if I had scaled it back down. I opened Heroku’s dashboard and found out that I had failed to scale it down. I was really upset and I checked my account and I saw that I was going to be charged $71 for October as well. I really did not know what to do. 8k for two months for doing nothing is something that is unacceptable. I should have been more careful. I also knew that Heroku would send out an email notifying the usage. I missed that email somehow.

I sat down trying to come up with some explanation to make me feel better 😀 and then the thought of actually contacting Heroku to ask for help struck me. So I opened a support ticket requesting them to help me out. I had not made even a single request to that app and so I had some faith that I would get something out of it. I got a reply from @rhyselsmore saying that the billing team is looking into my issue. At this point the way I looked at Heroku completely changed. It was some hosting company to me before that. All that came to my mind when I thought of was the blue website and the clean and modern dash and the amazing interface to scale your infrastructure and the super awesome CLI tool. But now there was a human touch to it. I realized that Heroku is run by humans and they are awesome people who help those in need! So my confidence rose.

Couple of hours later I got the amazing news from a person called Jen saying that they would cancel the October bill provided I be careful in future. It felt amazing. I was incredibly happy. This maybe something pretty trivial but for me it was quite amazing. Heroku helped me save 4000 rupees and changed the way I look at them and the other amazing tech companies that make the lives of developers enjoyable and comfortable. Kudos to Heroku!

Docker for Dummies

I came across this wonderful new tool recently through my boss. Although it took time to taste it (atleast to some extent), I soon realized the awesomeness of Docker. So I wanted to write a blog on what Docker is and the Dummy’s guide to using it (no offence intended to the readers ;). So what is Docker? Here’s what says,

Docker is an open-source project to easily create lightweight, portable, self-sufficient containers from any application. The same container that a developer builds and tests on a laptop can run at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more.


Continue reading

Making a comeback!

Its been over 10 or so months since I wrote a blog. I love the blogging culture. Its something everyone should try. We get so much out of blogs written by others but we never think about returning the favor. Well, I am actually talking about what I was doing these past 10 months. After I started developing Rails applications all I have been doing was reading article after article, several pages of documentation and tons of code. I learned so much from other people. I’ve faced hundreds of day to day problems from authentication to real time updates. But I never cared to write even a single blog about any of this. A friend of mine told me that he was going to try Rails and asked if I had good tutorials written in my blog. That question made me think. I’m not the greatest Rails developer on earth. And I’m not a bad one either. But I love Rails and Ruby. And if I truly love Rails then I shouldn’t just sit tight and keep churning out code. I should rather be spreading the love! So I started asking myself whether I truly love Rails or if it’s just a crush that’s going to fade away after few months like the ones I’ve had with C# and .Net, Java, Android etc. (I really don’t like the direction this blog is taking!). But I also knew that if I did not love Rails truly I would not have let go of two huge opportunities to work at large corporations and joined a totally unstable startup that was formed over a cup of coffee and few handshakes. BTW, that startup went south after few months and now I am in another awesome company. Ill talk more about this in the coming blog.

Alright. Now I know for sure that I love Rails. But the only problem is that I’m not doing anything about it. I haven’t contributed to Rails or other open source projects except to a couple of gems. So basically I haven’t done anything. That is why I’ve decided to start writing again. What I write might be very trivial for some but may make the day for others! Who cares as long as atleast one person gets benefited. That’s what the blogging culture is all about. Right? I’ve promised myself that I would post four blogs in a week. Don’t know if its too ambitious. But lets see. I wish the buses in our country had enough space to use a laptop comfortably. Funny thing is I think about this while I’m standing in the bus without a place to sit! Anyway lets see how it goes. And I realized today that my theme is so boring. Need to find a good minimalist design. That’s the way way I like it.

Id like to conclude this blog by telling you folks about the beta release of! Its a platform where you can share the various street level issues you face in your day to day life. It can be anything from broken roads to stagnant water and from voltage fluctuation issues to garbage disposal problems. You can locate the issue on a map and provide a rating for it. You could also attach a photograph to help describe the issue you are sharing. Its currently in beta and lacks few features. We are working hard on it whenever we get spare time and will have a decent release by the end of next week. We think and hope that this application will help start a revolution that would eventually help the government take note and cleanup our streets and roads.

That’s all for now. Cya folks!

Generate Entity-Relationship diagrams from Rails ActiveModels

Hey people! Documenting your code is really crucial for projects regardless of the size of the project. There are a lot of stuff that must be documented in a project. You have various tools to generate documentation for your code. I am going to show you a tool, a gem, that allows you to document your database models. When I was doing my Pacman ( I used an online uml tool called “dbdsgnr” to document my database. But I had to make many chances to my database schema and updating the Entity Relationship diagram I had on that website was getting tedious. At one point I dropped the whole idea of maintaining that piece of documentation. When I completed my project (pending testing), I wanted to document my database and I was searching for a tool that would document my rails project by reading the ActiveRecord models that I have. That was when I found this really cool gem called ‘rails-erd’.

This gem is maintained at github/voormedia/rails-rd and the project website is Installation of this gem is pretty easy. It depends upon a graphics tool called Graphviz.

You can install graphviz on Ubuntu using the command,

sudo apt-get install graphviz

Now add ‘rails-erd’ to your project’s Gemfile under the development group. So it would look like,
group :development do
gem "rails-erd"

Now run ‘bundle install’. This will enable your project to use the rails-erd gem.

We have installed the gem and its dependency. All we have to do now is to run a simple rake task to generate the ERD. The command is,

rake erd

This will generate “ERD.pdf” file in the application root. Neat huh? The best thing about this is that you can customize the way the output is generated by passing in many options. The various customizations are clearly explained in the project’s website. But mostly you wouldn’t need much customization. And I can tell you, the output is quite awesome. The entities are spaced well and the overall alignment of the diagram is quite brilliant. It looks like as though someone had manually aligned the entire thing. Okay, you’ll know what I am saying when you see the output pdf that I got for my project. Here it is. Click the image to get a better view.

See? Its just awesome. For more information regarding the gem visit


Installing Ruby, Rails, MySQL and Apache on a fresh Ubuntu 12.04 installation

The first challenge I faced when I started using Ubuntu was moving my Wubi installation inside Windows to a separate partition. Thats when I understood how one should find his/her way around the problems they face using Linux. Linux has one of the biggest support base on the internet. You can find solutions to almost all problems you face in the web. The only thing you need to do is a simple and proper Google search and you’ll fix whatever problem you have in not time at all! But unfortunately many who are new to this whole idea find it really difficult using Linux. This is because they are completed immersed in and used to the way Windows works. And so whenever they face some problem in Linux, like a dependency issue, they call you or text you and ask you to fix their problems. Unlike in windows where you get generic error messages with an error code, Linux provides verbose details about the kind of error that has occurred. People must make use of this information to find solutions to their problems.

Okay enough said! When I started learning Ruby I wanted to install Ruby on my Ubuntu 11.10 installation. But I really didnt know that I already Ruby pre-installed! Not knowing this I started installing Ruby and I faced a lotta issues. I had no idea what was happening. Then I tried removing Ruby. Then after my failed attempts to remove Ruby completely I installed RVM. Then I installed a Ruby version via RVM. But then gems like rails etc just wouldn’t install. Me being a Linux beginner back then found it really irritating and annoying to the extent that I considered dumping the whole idea of learning Ruby. But the pure elegance of Ruby and the thirst to learn Rails made me keep pushing. Finally after two days I got Ruby and Rails working on my PC without re-installing Ubuntu :P. Since then I’ve made several Ruby and Rails installations on many computers. I faced a lotta issues, especially dependency problems when installing Ruby, Rails, MySQL etc. So I decided to write this post to tell those beginners out there, the stuff they would need to properly install Rails etc.
Continue reading