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:

Hash[](args)

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:

Hash[1,2,3,4]

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!

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 www.changebadtogood.com! 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!

A handy tool for our college staff


Hello people!! Its been a really long while since I posted anything here. Its been a really good 6 or 7 months for me both academically and personally. Been through a lot of changes, learnt new stuff, decided on my main field of interest etc! Well… the main thing is, Im Back! Ive jumped over to the Ruby on Rails platform now. And I think I love this platform. Its a joy to write code in Ruby and extra joy to use the goodies provided by Rails.

I also have undertaken a project for my college to manage marks and attendance which is almost complete. I will write an exclusive post about that in coming days.

Ok. Before I begin this post I cant help mentioning how good Chelsea are performing this season! 😀 We’re on course for a Title win! 🙂

Ok okay… My college’s management is very demanding. When our university results come, the staff are required to get the results from the results websites, analyse the results and generate reports on the very same day which can be a really challenging and tiresome task given the large number of students in each batch.

I always wanted to help then out by giving them an easy way around this cumbersome job. Ive tried finding loopholes and vulnerabilities in the University Server but hit dead ends all the time. Finally I decided that the only way (atleast for me) to get the results was to parse the results from the website directly from the HTML page. I had this idea in mind but did not implement it until the results came for my 6th semester!

That night I was reading stuff when one of my friends texted me saying that the results have come. I opened the results website without any sorta excitement or expectations 😉 coz I already know that Anna University (the uni to which my college is affiliated to) is known for throwing big surprises. And they did not dissapoint! An average result once again (for me). Well I had to do something to divert my mind. So I thought of writing the app to download the results! 😀 And my word! It was a brilliant idea 🙂
Continue reading