Sunday, May 24, 2009

Regular expression Ruby

Hi Guys,

This post is about my stint on regular expressions using Ruby, programming language. Ruby is one of the powerful languages that provide excellent support to Regular expressions.

There are several method available for String class for checking the patterns. I am gonna write about few of the methods available and also that I know.

Now let’s talk about how can ensure that email is in right format. so let’s take an email id barrack.obama@gmail.com.
Now our aim is to find the word before and after the symbol @. Ruby provides a method for scanning through string using regular expression called scan.


s = ‘barrack.obama@gmail.com’
s.scan /[\w+\.]{2,20}/


The above expression will result in an array as following,
[‘barrack.obama’, ‘gmail.com’]

See how easy it is. This expressions are not limited to ruby language, this is the same for all languages. This expression will work in most cases.

Let me explain the expression I have written above. [\w+\.]. what this says is that it will try to match to match words that includes underscore and dot. {2,20} means that the word that gets matched should have the length of 2 to 20.

This expression will help you to get the domain name of the user this email address belong to.

The scan method will always result in array, which might not be what we want all the time. There can be situations where we want to check if the string matches the certain pattern like if or while or case loops

For those cases, ruby provides a way to do that using a new operator =~.

Let’s assume that we want to check if the file has an extension .jpg.
image = ‘bacd.jpg’

you can always do like


image =~ /\w+\.(JPG)$/i


The expression mentioned above matches any string that starts with and ends with .jpg irrespective of the case.

There is one powerful ruby method for searching and replacing the set of words called ‘gsub’. Let’s assume we have a word ‘hello123’ and we want to replace 123 with ABC. This can be accomplished easily with gsub. The following syntax


litter = ‘hello123’
litter.gsub(/\d+/, ‘ABC’)


The above statements will result in ‘helloABC’. There are lots of things in ruby which I haven’t explored yet. Those may follow next in the post.

That’s all for this post I have...

Monday, April 6, 2009

Yahoo Hack Day 2009

I wanted to write something about the yahoo day which I attended few days back. I had initially thought like it will be a event which would be about hacking into yahoo's products and finding the bugs in it. I really didn't think it would be about using Yahoo API's to develop applications based on that.

It was a beautiful Saturday that started with the usual marketing stuff from Yahoo guys. I was surprised to see range of products that yahoo has. There is one thing evident. Yahoo is clearly struggling to compete from Google's products. It is not that Yahoo doesn't have good products. One of the examples can be Yahoo Go that is what I use regularly for me needs. According to me, Yahoo's products lacks the simplicity that the professionals expect. The example can be the mail platform.

After all this, started the hacking. For me it looked like deluge of professionals came together to create great applications. I could see everybody was active and the discussions happening around great ideas.

At that moment, I was thinking like what I could do. The following link was the result of the event.

1. antony-raj.appspot.com
2. antony-raj.appspot.com/dzone

I never thought that I would know Python and code a decent application using Python. Please don't forget to check the second link. You will cherish it forever.

Monday, January 12, 2009

Recursing through Directories in Ruby

I was trying to print the directories and files recursively, this task was daunting, for me, in Ruby, as I am from Java. I was trying to do the same way that the way we do it in Java. During this course of time, I tried in several ways. Now, the way I implemented is different, Ruby way.

Here our task is to print only the files and its size. In ruby, the code is as follows.

require 'FileUtils'
class DirectoryProbe
def self.print_all_files filename
if File.directory?(filename)
Dir.chdir(filename)
files = Dir['**/*']
files.each{ |file|
unless File.directory?(file)
puts "#{file} \t #{File.size(file)} bytes"
end
}
end
end
end


Dir.chdir(file) issues a command cd in windows and linux. The code Dir['**/*'] returns the array containing all the files in the directory recursively. After that I work on each file separately.

Wednesday, January 7, 2009

Decorating Your Mac OS X

Are you new to Leopard, then this post is for you, now read on...

In Thoughtworks, I was given MacBook Pro just few weeks back. I always heard that MacBooks are great. But I was really skeptic about it. I had questions like when Apple can do it why not Ubuntu or Microsoft guys. So this post is about how my MacBooks is and few must-know shortcuts for MacBook.

Though I have experience working with Ubuntu, I cannot actually say Ubuntu is best after working with MacBook. It is not an underestimating statement about Ubuntu. Ubuntu very well provides the animation capabilities exactly similar to MacBooks. The reason is that Ubuntu lacks several things naturally like rendering of windows, fonts etc. The color it has chosen for windows looks so gloomy. In psychological terms, the brownish color brings you a nice sleep even if you are very much awake. Here Microsoft windows wins to its extent. So let's request Canonical to bring Ubuntu with the color that is very pleasing to our brains. We should be already thankful to Canonical for its effort to bring Ubuntu as a delightful Linux distribution, and the best choice of operating system next to Mac OS X.

As you are a beginner, let's decorate our MacBook with the following applciations.
Firefox - World's best browser with extensions of your choice.
QuickSilver - It is like SpotLight, once you use it, You will never uninstall it, this application is quite great.
Folx - The only good download manager with resume support, though still in beta.
Real Player - The one of the good players apart from ITunes and QuickTime. It comes with media download manager, which automatically provides great integration with Firefox. So you don't have to install any special download manager extension for Firefox. Whenever you encounter an media file in the page like youtube site for example, it automatically notifies whether you wanna download that file. So it is fantastic applicaiton.
Vienna - It is the best open source RSS application
Adium - The best internet messenger for Mac, it support yahoo, gtalk etc.
VLC - The unbeatably best media player on the earth.

If you are a programmer, you might prefer to install the following applications too
Smultron - good html editor.
TextMate - The best mac Editor for almost all the languages.
Eclipse - All in all better and open source programming IDE.
Netbeans - The best dynamic languages IDE.
VirtualBox - the platform agnostic open source virtual player from Sun Microsystems. Install this, if you prefer to install Windows or Linux in your Mac Machine.

These are some of the reguarly and very frequently used shortcuts in Mac

show the desktop - F11
Show the dashboard - F12
Force Quit - Command + Option + Escape
Logout - shift + command + Q
Logout immediately - Option + shift + command + Q
Minimize Window - Command + M
Minimize all Windows - Option + Command + M
Inverse the color - Control + Option + Command + 8
Hide the Dock - Option + Command + D
Move the File - Command + Drag
Screenshot - Command + Shift + 3
Shutdown Dialog Box - Control + Eject
Restart the Machine - Control + Command + Eject
Sleep - Option + Command + Eject

Tuesday, December 9, 2008

Intellij - Ubuntu Does this combination work?

Hey Ya, It works.

The most intelligent Java IDE on earth is definitely Intellij Idea. I was in a plan of migrating to Ubuntu because of the nature of Ubuntu. It looks great, perfect fit for me as I knew a bit of linux administration with its forefathers (Debian). It is quite faster too than its competitor Windows(Maharaj), Will talk about why Ubuntu later on in a different post.

As a programmer, I faced lots of difficulties in Ubuntu. My immediate need was to have a perfect editor to fit my bill. Then I installed Jedit. Though Gedit is good enough, as I wanted to lots of functionalities, i went with it.

When I installed Intellij Idea in Ubuntu it looked terrible. If you are not concerned about working in Ubuntu, that would be the day you will work last on Intellij Idea in Ubuntu.

So here is mentioned what you have to make it look great.
Under IDE Settings select the Look and Feel of Intellij, Choose Nimbus, it is the open source and modern theme developed for Java. So Now IDE looks great.
Then we have to tweak fonts that appear over Intellij. Now go to appearance under Editor, select checkbox for using anti-aliasing effect. Now the fonts look good to certain extent.
For making things look great issue the following command in the terminal

sudo apt-get install ttf-liberation

Now this will install liberation fonts. If you wanna check how liberation fonts looks like follow this link Liberation Fonts

You may want to install microsoft specific fonts in Ubuntu, for that, issue the following command,

sudo apt-get install msttcorefonts

You may set Arial 12 for the IDE appearance.

Once you install this font, restart your IDE. During restarting, Intellij essentially reloads the fonts installed in your system. Now Settings --> Editor --> Colors & Fonts ---> Fonts
Now select Liberation Mono and the size is 13 and spacing is 1.5.

Now it looks awesome. Isn't it?

Tuesday, July 1, 2008

Surprise GWT Visit

Guys,

This post is about my encounter with GWT (Google Web Toolkit). It is really a wonderful product from Google from the developer's perspective.

The benefit as a developer what I can say is, it provides you a way to debug in the hosted mode in the way we do debugging in Eclipse. You don't have to worry about getting into javascript hassles. Everything you can do right from Java IDE. Again for the modification to reflect, you just need to refresh the browser that GWT came with. If you would like to run your GWT application from weblogic server or any other server, you can always use the option -noserver in the shell. If you are bit familiar with the way that Swing works, you can become a great programmer with GWT. I don't find it appropriate to develop GUI with GWT, rather than going with creating HTML and adding javascript on top of that. My question is that why would Google try to challenge the way we write the web pages now. Even for the simple change in GUI we may have to look forward to changing the Java file and compile it. I guess this might be appropriate for developing the simple and small application. Though I agree that you can develop the HTML and on top of it you can plug in GWT apps in certain tags. I found it not welcomeable.

When you integrate GWT with frameworks like Spring etc, you need to make some more effort. In this time you need to seek the help from utilities like GWT-SL and GWT-WL. But this time you will be losing the facility provided by Hosted mode that GWT boasts of, especially debugging. Though it can be enabled using proprietary servers like Weblogic, Websphere, etc. I tend to think of tomcat server, which is my favorite, I will tell you why too.

One of the hassles that GWT gave, when i was trying out is that my page had had two gwt placeholders, one of which contained main app, and another one small placeholder where I had planned to show a PopupPanel. when I integrated both of these panels, I couldn't show popup panels as expected. But when I had added only one panel to my page, it showed brilliantly. The cause is that it was not firing events for button clicks and so on. And in the DiaglogBox of GWT, you can have only one panel, even though you can make it that way. Anyways I was not impressed much with the GUI styles provided by GWT. For the sake of it, I tried to use ExtJS for GWT. It was highly impressive. It addressed all of my issues very well. But, when I started using it, I realized that though it was an open source library, it had a dubious license, meaning that when you develop applications using GWT - Ext Js, you should keep your complete code of your application open sourced. For my home work, it sufficed, but I couldn't recommend for my clients to use it due to its policies.

So at last, for my project with my client, I ended up recommending Dojo with DWR (Direct web Remoting) . I find this combination appropriate. The reason is that Dojo addresses all the GUI related functionalities. DWR addressed the functionalities like serialization and deserialization of java objects into javascript.

I initially recommended Open Laszlo for our work as it is sufficient to address almost we can imagine, I couldn't recollect why it was rejected. If you see the functionality provided by Open Laszlo, it gives two stark options, one is that it converts the complete GUI as Dynamic javascript and if your browser has Flash plugin installed( I don't think somebody wouldn't have), it will show your application as flash based application, which I consider as appropriate for most of the possible situtations. On top of Open Laszlo, you have lots of add ons too.

We ignored most of the proprietary RIA tools because our client was not inclined to use any proprietary technologies. I prefer the same too.

So what is next? I have to burn my hands in Dojo and its internals and DWR and its internals. My project is going to start in a week, I need to prepare myself for it.

See you soon once again with some more nice information.

Monday, May 26, 2008

Interview programming challenge 5#

This post gives you answer for the question posted at

http://www.dev102.com/2008/05/26/a-programming-job-interview-challenge-5-records-sorting/

You are asked to sort a collection of records. The records are stored in a file on the disk, and the size of the file is much bigger than available memory you can work with. Assume a record size is 10 bytes and file size is 1GB. you read and write data to/from disk in units of pages where each page is 1KB big. You have few (less than 10) available pages of physical memory. Assume you are given a method to compare two records.

How can you sort the records under the described conditions?

* Hint - Using the virtual memory is not the correct answer.


The appropriate solution for this problem would be to use algorithm based on Insertion Sort