Friday, May 21, 2010

Do you want to touch real Tigers ?

Last week my wife and I visited Thailand. In the tour, we got a chance to visit Tiger Temple near Bangkok. Yes ... the thrill was patting real tigers. Tiger Temple, or Wat Pha Luang Ta Bua, is a Buddhist temple in Western Thailand where tourists can actually touch/pat the tigers in open area. It was a great experience in watching the real tigers staying with men and how the people take care of them. The tigers are handled by Thai monks, volunteers and the local staff.

Sharing few photos ...



Thursday, April 22, 2010

Ruby metaprogramming

This is an excellent video about ruby metaprogramming concept:-
http://www.infoq.com/presentations/metaprogramming-ruby

This was presented by Dave Thomas, a well known author of The Pragmatic Programmer book.

Tuesday, April 20, 2010

Passing hash from javascript

I was working on one functionality where I needed to build hash like structure in javascript and make an ajax call to perform that particular action by passing that hash as params to process further. The functionality was to delete a numbered row from the form and after deletion the form should rearrange the rows in sequence while maintaining the order. I used javascript 2 dimensional array to treat it as hash.

e.g. On the form :-

No. | EmpID | Date | Location | Action |
1. | 123 |12-Mar-10 | Mumbai |Delete |
2. | 233 |10-Jan-10 | Mumbai |Delete |
3. | 444 |15-Mar-10 | Mumbai |Delete |
4. | 555 |18-Mar-10 | Mumbai |Delete |

So. If you delete the 3rd row, then after deletion the new form should render as :-

No. | EmpID | Date | Location | Action |
1. | 123 |12-Mar-10 | Mumbai |Delete |
2. | 233 |10-Jan-10 | Mumbai |Delete |
3. | 555 |18-Mar-10 | Mumbai |Delete |

The elements were written in table format. Each row is assigned unique id, so first row will have id = 1, 2nd row with id = 2 and so on. Each td has unique class.

<%=row_id%>
<%= text_field_tag "emp_#{row_id}", "", :id => emp_#{row_id}" %>
<%=text_field_tag "dt_#{row_id}", "", :id => "dt_#{row_id}" %>
<%= text_field_tag "loc_#{row_id}", "", :id => "loc_#{row_id}", %>
<%=link_to(image_tag("delete_img.jpg", :alt => "Delete Row", :id => "del_#{row_id}"),"#")%>

This is the jquery code :-

jQuery(document).ready(function() {
jQuery('#del_<%=i%>').click(function() {
var row_to_be_deleted = jQuery(this).parent().parent().parent();
var row_id = parseInt(row_to_be_deleted.attr("id"));
var data_ary = [];

jQuery('#emp_table tbody > tr').each(function() {
if (jQuery(this).attr("id") != row_id) {
var myArray = [];
var empid_val = "";
var dt_val = "";
var loc_val = "";
jQuery(this).find("td").each(function(){
if (jQuery(this).attr("class")== "empid"){
empid_val = jQuery(this).find("input").val();
myArray.push(empid_val);
}
if (jQuery(this).attr("class")== "dt"){
dt_val = jQuery(this).find("input").val();
myArray.push(dt_val);
}
if (jQuery(this).attr("class")== "loc"){
loc_val = jQuery(this).find("input").val();
myArray.push(loc_val);
}
});
data_ary.push(myArray);
}
});
ajax_call_for_remove_row( data_ary, '<%= remove_row_controllername_url%>');
});
});

----
function ajax_call_for_remove_row(data_ary, remove_row_url) {
var inputs = new Object;
inputs["data_ary"] = data_ary;
jQuery.ajax({
url: remove_row_url,
data: inputs,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("We are sorry something went wrong, please try again");
},
success: function(data){
},
type: "post"
});
}

----
The corresponding controller actions :-

def remove_row
@data = []
logger.debug(params['data_ary'].inspect)
build_data_from_hash(params['data_ary'])
logger.debug(@data.inspect)
end

private

def build_data_from_hash data_hash
if data_hash
data_ary = sort_hash_to_array data_hash
data_ary.each { |value|
@data << {"Empid" => value[1][0], "Date" => value[1][1], "Location" => value[1][2] }
}
else
@data << {"Empid" => "", "Date" => "", "Location" => "" }
end
end

def sort_hash_to_array data_hash
data_hash.sort { |a,b| a[0].to_i <=> b[0].to_i }
end

----

params['data_ary'].inspect =>
====================================
{"0"=>["123", "12-Mar-10", "Mumbai"], "1"=>["233", "10-Jan-10", "Mumbai"], "2"=>["555", "18-Mar-10", "Mumbai"]}
====================================

@data.inspect =>
====================================
[["0", ["123", "12-Mar-10", "Mumbai"]], ["1", ["233", "10-Jan-10", "Mumbai"]], ["2", ["555", "18-Mar-10", "Mumbai"]] ]
====================================

We can pass this @data as local while rendering the table body partial in corresponding js.erb template. This works great for me!

Tuesday, April 13, 2010

RubyConf 2010 - Bangalore, India

In the last month, I got an opportunity to attend the very first Ruby conference held in Bangalore, India(20-21 March) organized by ThoughtWorks. The response was very huge (around 400 people ranging from beginners to CEOs from more than 100 companies) and people really enjoyed the event.

It was really great to hear some good speakers/industry leaders like Matz, Obie Fernandez, Ola Bini, etc. Many technical topics were presented like future of Ruby, Rails 3.0, Glassfish and WebRoar app servers, building cross platform mobile application with Rhodes framework, etc. You can find more details about these topics at http://rubyconfindia.org/

I felt a lot of enthusiasm and energy amongst the people w.r.t. Ruby and Rails. There are many small companies being set up especially in Pune and Bangalore for doing only rails projects. I really liked that !

Indian market is really catching up on Rails very fast !

Rendering partial with jquery

Below is the way by which you can render partial with jQuery in your view.js.erb file :-

jQuery("#mydiv").html("<%= escape_javascript(render(:partial => 'my_partial', :locals => {:my_instance_var => @my_instance_var} ))%>");

Friday, March 19, 2010

A mechanic and a surgeon

A mechanic was removing a cylinder head from the motor of a Harley motorcycle when he spotted a well-known heart surgeon in his shop. The surgeon was there, waiting for the service manager to come and take a look at his bike. The mechanic shouted across the garage, "Hey, Doc, can I ask you a question?"

The surgeon a bit surprised, walked over to the mechanic working on the motorcycle. The mechanic straightened up, wiped his hands on a rag and asked, "So Doc, look at this engine. I open its heart, take valves out, fix 'em, put 'em back in, and when I finish, it works just like new. So how come I get such a small salary and you get the really big bucks, when you and I are doing basically the same work?"

The surgeon paused, smiled and leaned over, and whispered to the mechanic...
"Try doing it with the engine running."

Friday, February 12, 2010

WebROaR - Ruby Application Server

I got a chance to visit http://webroar.in/ and it seems interesting.

http://webroar.in/blog/2009/11/25/introducing-webroar-v0-2-3-ruby-application-server

WebROaR is an application server that makes deployments of ruby web applications extremely simple. It provides an integrated solution to view the run time performance numbers and email notifications in case any exceptions occur in any of the deployed applications. It is 5 to 55% faster than all other comparable deployment stacks for RoR apps.

Comparison :-
http://webroar.in/blog/2009/11/25/comparison-of-rails-deployment-stacks-2

Some screenshots :-
http://webroar.in/screenshots

Source :-
http://github.com/webroar/webroar/

Wednesday, February 10, 2010

Scientific explanation of Bhagwad Geeta

I recently read a very good book written by Dr. P.V.Vartak - "Geeta - Vidnyananishtha Nirupan" (Scientific explanation)

As we know, Geeta is a conversation between Lord Krishna and Arjuna which took place on the battlefield of Kurukshetra just prior to the start of Mahabharata war. Responding to Arjuna's confusion and moral dilemma, Krishna explained to Arjuna his duties as a warrior and real path of truth and elaborated on a number of different Yogic and Vedantic philosophies, with examples and analogies. Geeta is often being described as a concise guide to Hindu philosophy and also as a practical, self-contained guide to live life especially with Karma yog. Karma Yog is a science that frees us from the bonds of actions and attachment with the attributes. According to the Bhagawad Geeta, it consists of mentally renouncing the sense of doership in favour of God while performing all actions.

Dr. Vartak has explained all the 18 chapters (addhyay) of Geeta in a very simple but superior language. Many thanks to him !

He has also pointed out some mistakes that Mahaatma Gandhi (M.K.Gandhi) did which ultimately led to a lot of violence during Indian independence. Mahaatma Gandhi used to daily worship/read Geeta, but unfortunately could not grab the real meaning of non-violence, Karma yog described in Geeta. The Geeta was actually told to Arjuna to take away his confusion and encourage him to perform his duties as a warrior. But Mahaatma Gandhi never understood the crux of Geeta and repeatedly made mistakes in terms of following non-violence, forgiving real enemies at National level and unfortunately the Nation also followed wrong leadership during that time.

I would request everyone to read this book and think in solitude on the thoughts explained in the book ... We have yet a lot to improve ...

Saturday, February 6, 2010

A rumour takes wings

A pandit crossing a field felt that there was something in his mouth and spat it out. It turned out to be a heron's feather. He could not understand how it had got into his mouth and it perplexed him a great deal. When he reached home he told his wife about it but asked her not to tell anyone lest somebody put a bad interpretation on it. His wife was even more intrigued by the strange occurrence and felt the need to confide in someone. So she swore her neighbour to secrecy and told her what had happened.

Perhaps it was the way she told it, but her neighbour got the impression that several feathers had come out of the pandit's mouth. She was shocked. However, she assured the woman that such things could happen and advised her not to worry about it.

"Please don't tell anyone," said the pandit's wife.

"My lips are sealed," said the woman. But she was longing to tell someone and when she saw the dhobi's wife going past, called her in and told her the whole story. Only, she made it sound as if a whole heron had come out of the pandit's mouth.

"Never have I heard of such a thing," said the dhobi's wife, her eyes popping with excitement, "and he being a vegetarian and all that, but one can never tell..."

She went away promising not to tell anyone but on the way she met her friend and the whole story sort of tumbled out of her mouth. Perhaps in her excitement she said 'herons' instead of 'heron' or perhaps her friend just imagined she had said herons but when she told her husband the story sometime later, she was emphatic that a whole flock of herons had come out of the pandit's mouth.

And as the story spread "herons" became "herons and other birds" and then "hundreds of birds of all shapes and sizes".

By evening the whole village and several other neighbouring villages had heard the story and people began to arrive in droves at the pandit's house to witness the miraculous happenings there.

The pandit steadfastly denied that any bird had come out of his mouth but nobody would believe him and everybody begged him to demonstrate his wonderful power of producing birds from his mouth.

Finally in exasperation, he asked them all to sit in front of his house and when they had done so ran out of the back and hid in the jungle where he remained several days till the excitement had died down and the people had realised that the news was false...

Monday, February 1, 2010

email_pop_reader gem pushed to gemcutter

In one of my previous projects, I worked on ruby script to read the emails from mail-in database and process the emails as per the business requirement. In ruby, we have a libray called Net::POP3 which provides functionality for retrieving email via POP3. I went ahead with a thought of converting this to a gem.

The emails retrieved from mail-in database are stored in some sort of data structure to process further. The utility makes use of Net::POP3 and TMail libraries and provides with some handy methods such as 'retrieve emails' as array of hashes. The hash has email's from,to,cc,bcc,subject,body fields. Email body with attachment has not been considered for simplicity.

It also provides 'delete_emails(unique_email_ids=[])' method which takes array of unique email ids (retrieved with pop email unique_id) as parameter and deletes those.

Install :-

gem install email_pop_reader

(It has been pushed to http://gemcutter.org) - http://gemcutter.org/gems/email_pop_reader

OR

Download the gem file from http://github.com/NiranjanSarade/email-pop-reader.git/
gem install email_pop_reader-0.0.1.gem