Saari Development

This blog is intended to be a log of my (Ali Rizvi's) professional ramblings as a software development engineer. I intend to add logs of my experience with different technologies, software, tech books/articles and related stuff from time to time. My intention is to have an archive for my personal use and public benefit.

Tuesday, November 28, 2006

Windows: Making my laptop *nix friendly

I usually do most of my development on my Linux machine and only use my Windows XP laptop for using Outlook (mail and calendaring) and browsing (Firefox, of course).

I decided to work from home due to weather conditions and had to use my laptop more and finally took the steps to make my laptop more unix friendly.

I already had my favorite editor Vim installed on the machine and ruby one click install (which did not required the forbidden admin priviliges thank God) made quick experimenting easy while I was commuting to work. Also rubygems installation work pretty smoothly (similar to on a linux/mac osx system) on windows also.

I already had configured my laptop to use have tab completion for windows lesser shell (cmd or Command Prompt) so cd into directories was a breeze.

Two things that I missed the most still were ls (which I often typed and got error on windows shell) and typing vi filename and opening file and I was fed up with using edit to open files all the time which is far from a decent text editor at least by my standards.

Finally I was motivated enough to do something about these. Here is what I did:

Added the path to the directory where vim was installed to my environment variable:


1. Right Click on 'My Computer' and click 'Properties'
2. Click on 'Advanced' tab and click 'Environment Variables' button, click 'PATH' if already exists or create new if it is not already there.
3. Add the absolute path of directory where vim.exe exists at the end (semi-colo {;} separated list).

Create shortcuts for vi and ls


1. Now open command prompt (Start -> Run -> cmd -> OK)
2. cd into the directory where vim is installed (e.g. C:\>cd "Program Files\Vim\vim70")
3. edit vi.bat
4. type "C:\Progra~1\Vim\vim70\vim.exe %1 %2 %3" save and exit (Alt - F - X) the file
4a. %1, %2 ... are the arguments passed to the bat (shell) script
Since I am lazy I will create the ls bat also here but you can create it in any directory in your path
5. vi ls.bat (now you can use your new shortcut)
6. type "dir %1" save and exit (:wq) the file

Enjoy!

Monday, November 06, 2006

Ruby: Finding the absolute path of running script

I needed to use the full/absolute path of the script/program to a library for access control. I had not done something like this before so here is my trail of discovery:
$0

I started with $0, which in ruby is one of those magic variables that contain the name of the program that was executed from the command line.

The problem with $0 is that it does not necessary have the absolute path, it has whatever the user used which could be relative or absolute.
__FILE__

Then I found __FILE__ which was not exactly what I was looking for as it would give you the file name of the current file so when executing library routine it would be the name of the library file not the original executable file that the user ran.
File.expand_path

After some research I found File.expand_path which essentially did the trick if you use it with $0. So File.expand_path $0 would give you the absolute path of the calling program.
Pathname.new.realpath.to_s

On a parallel note I found Pathname.new.realpath.to_s also but that require you to require 'pathname' and also it resolved a symbolic link to real path which was not desired in my case.
Dir.chdir

One caveat with File.expand_path which an experienced rubyist pointed out that if a script does a Dir.chdir then File.expand_path would not work as it essentially prepends the cwd (current working directory) to $0 but even the fellow rubyist couldn't think of a better way to do this so File.expand_path was my eventual solution and it works!