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