Windows Command Line

Ruby is a scripting language and most of the time you will use it from the command line. For years Windows has been known for its poor command line interpreter. Although Microsoft has improved it in Windows XP and later versions, it still lacks some features which come in very handy when developing Ruby applications. In this chapter you'll learn how to use Command Prompt and adjust it for Ruby development.

Click on the start button in the lower left corner, then on All Programs and Accessoriess. In the list of applications you'll see Command Prompt.

""

Click on it and new command prompt window will open. It is time to make first adjustment. Normally Windows command prompt uses raster fonts and cannot display Unicode characters. Click on the icon in the top left corner of command prompt window and then on Properties menu item (Figure 1-2).

""

In the properties dialog box activate Font tab and select either Lucida Console or Consolas. These two fonts are TrueType monospaced fonts and can display characters from Unicode character set. Font size doesn't matter so you can choose the one that suits you the best.

Running application from the Command Prompt can be done either by giving full path to the executable file

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\>c:\Windows\notepad.exe

or, if the executable file is in the path just invoking application by executable file name:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\>notepad

Let's stop here for a second and see what path actually is. The path to a file or a folder is its address on the computer. On Windows operating systems path consists of a drive letter and all directories and sub-directories which determine location of a file or a folder in the system. Operating system uses path to find a file. In our case it means that system will search for notepad.exe file on drive C: in the Windows folder.

Invoking application by giving full path to the executable every time is quite inconvenient. Besides, if application loads dynamic libraries, system is searching for them in the application directory first and then in the path. Therefore if you want to use application from the Command Prompt it is recommended to put it in the directory which is in the system path. Whenever you issue command in the Command Prompt, system will search for the executable first in the current directory and if it doesn't find it, search will continue through all directories listed in the path. Folders are searched in the exactly same order as they are listed in the path. Now it is time to check what is a value of the path system variable. Open new Command Prompt and type:

C:\>echo %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem

As you can see we currently have only few system directories in the path. These directories will be searched whenever you issue command in the Command Prompt if executable is not found in the current directory. System will first look for the executable in the C:\Window\System32, then in the C:\Windows and finally in the C:\Windows\System\Wbem. Note that directories in the path are separated by semicolon. This separator must be used whenever you want to add new directory to the path as we'll see later.

Altering path for the current Command Prompt is done simply by invoking set command:

C:\>set PATH=c:\Ruby22
C:\>echo %PATH%
c:\Ruby22

Be careful not to type any spaces before or after the “equals” sign. Setting path variable obviously removed all other directories and left only the one we added to the path. This means you will not be able to run system applications just by invoking them. Let's try it:

C:\>notepad
'notepad' is not recognized as an internal or external command,
operable program or batch file.

As you can see system is not able to find notepad executable because it doesn't exist in the current folder and only directory which is in the path is C:\Ruby193 which actually does not exist, unless you created it manually. Before we go on with Windows path exploring we should fix it in the current Command Prompt:

C:\>set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem

Removing all directories from the path when new directory is added surely is not a good way to alter system path. What we actually want is to be able to add new directory without removing existing ones. As we already saw system will set variable to the value we pass after equal sign. This means we can just put our new directory and all already existing directories in the statement for setting system path. This requires lot of typing. Luckily there is a shortcut. Using system variable %PATH% allows us to keep existing directories while we are adding new one:

C:\>set PATH=C:\Ruby22;%PATH%
C:\>echo %PATH%
C:\Ruby22;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem

As you can see set statement keeps the order of directories and you must be careful when you set the path. If you want newly added folder to be searched first you must put it before %PATH% variable. Contrary if you want system first to search other folders and at the end to lookup in new added one you must put %PATH% variable at the beginning.

Changing path this way is temporary and as soon as Command Prompt window is closed all changes will be reverted. You can easily check this by opening two Command Prompt windows. Changes made in one of the windows will not be reflected on the other.

There are two ways we can permanently change system path. The first and easier one is to use System Properties dialog box. Open Control Panel -> System -> Advanced System Settings. Alternatively you can click on the Start button and right-click on the computer and choose Properties -> Advanced System Settings. When System Properties dialog box opens, activate Advanced tab and click on the Environment Variables... button (Figure 1-3).

""

New dialog box will open with two lists of system variables. In the upper list all user variables and in the lower one system wide variables are displayed (Figure 1-4).

""

Changes made to system variables will be visible for all users on the system. Contrary, changes made to user specific variables will be visible only to the user who's account is changed. If you change path I recommend keeping your specific changes in the user section. That way you will clearly keep changes you made separate from the other users. There is one more important thing you should know. If you define path variable in the user section, directories from that variable are added at the end of the system-wide defined path. This was the first way.

Second way is more advanced. Windows operating system keeps all system related configuration options and settings in the registry. Thus you can change system variables and among them the path, directly in the registry. System-wide variables and their values are stored in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment section. User defined variables can be found in HKEY_CURRENT_USER\Environment section.

Be aware of one big difference between changing path and other environment variables through System Properties Dialog Box and directly in the registry. Changes made within System Properties Dialog Box are followed by the broadcast message about change of system variables. This message is sent to all opened applications and it is up to application to handle this change. Unfortunately Command Prompt does not handle this message at all and in order to have Command Prompt which is aware of the change, you will have to close all open Command Prompt windows and open them again. Altering system variables through registry is low-level change and system will not broadcast message so no application will be aware of this change. In that case you will have to log off and log on to your account again if you want applications to be aware of path changes.