SoFunction
Updated on 2025-04-07

RUBY Document Center-Study Begins

First, you have to check whether Ruby has been installed. At the shell prompt (denoted as "%", so don't hit %), hit

% ruby -v  


(-v tells the interpreter to print out the version of Ruby), and then press Enter. If Ruby is installed, you will see a message similar to the following:

% ruby -v
ruby 1.6.6 (2001-12-26) [i586-linux]  


If Ruby is not installed, you can let the administrator install it, or you can do it yourself, Ruby is a free software that has no installation or usage restrictions.

Now, let's play with Ruby. You can place a Ruby program directly on the command line with a -e parameter:

% ruby -e 'print "hello world\n"'
hello world  


Generally, Ruby programs are saved in a file.

% cat > 
print "hello world\n"
^D
% cat 
print "hello world\n"
% ruby 
hello world  


^D refers to control-D. The above is the case under UNIX. If you use DOS, then that's it:

C:\ruby> copy con: 
print "hello world\n"
^Z
C:\ruby> type 
print "hello world\n"
C:\ruby> ruby 
hello world  


When writing more practical code than this, you will want to use a real text editor!

Some amazingly complex and useful things can be made with a single line of command mini program. For example, this thing replaces foo in all C source programs and header files in the current directory with bar, and adds a ".bak" backup of the original file:

% ruby - -pe 'sub "foo", "bar"' *.[ch]  


This program is similar to the cat command under UNIX (but slower than cat):

% ruby -pe 0 file