88Pro Thinking

cat /senthoor/mind | grep thought > blog

Sunday, September 11, 2005

My First Ruby Attempt

I was on Skype with Hasith after the Ruby session. One question he shot was what and when do you think Rails will fit in our company. I was not very clear with the answer then but I guess Dave from Pragmatic Programmer Inc has some what addressed the question in his latest blog.

My first Ruby script was for some simple SQL code generation. I was given a file containing phone numbers (10k of them) and I have to produce an output file which contains 15 delete SQL statements for each of the phone number in the file.

01: #Delete Array Contains the Delete Statements in the order of Execution
03: delete_array=Array.new
04: delete_array[0]="delete from senthoorvideo where userid='MSISDN';"
05: delete_array[1]="delete from senthoorphoto where userid='MSISDN';"
06: delete_array[2]="delete from senthoorphoto_bak where userid='MSISDN';"
07: delete_array[3]="delete from senthoorfolder where userid='MSISDN';"
08: delete_array[4]="delete from senthoor_sim_contacts where userid='MSISDN';"
09: delete_array[5]="delete from senthoordatalogger where userid='MSISDN';"
10: delete_array[6]="delete from senthoor_sync_mapping where userid='MSISDN';"
11: delete_array[7]="delete from senthoorphonebook where userid='MSISDN';"
12: delete_array[8]="delete from senthoorphonebookchanges where userid='MSISDN';"
13: delete_array[9]="delete from senthoorphonebookindex where userid='MSISDN';"
14: delete_array[10]="delete from senthoorphonebookmapping where userid='MSISDN';"
15: delete_array[11]="delete from senthoorphonebooksim where userid='MSISDN';"
16: delete_array[12]="delete from senthoorphonebooksynchresult where userid='MSISDN';"
17: delete_array[13]="delete from senthoor_search_history where userid='MSISDN';"
18: delete_array[14]="delete from senthooruser where userid='MSISDN';"
21: delete_input_file = File.open("MWFailedList-BothJulyAndAugust.txt")
22: output_SQL_File = File.open("delete_script.sql","w+")
24: delete_input_file.each do |line|
25: delete_array.each{|delete_sql| output_SQL_File.write(delete_sql.sub("MSISDN",line.strip)+"\n")}
26: end
28: delete_input_file.close
29: output_SQL_File.close

Did you know that
1) reading a file
2) taking each element
3) run throuh an array
4) replace a token with an element
5) write back to a file

can be done in just two lines of code? (check line 24,25)

posted by 88Pro / Sunday, September 11, 2005

This page is powered by Blogger. Isn't yours?

doteasy.com - free web hosting. Free hosting with no banners.