Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby - Getting Started

What is ruby?
Prev
Contents
Contents
Simple examples
Next

 Ruby user's guideGetting Started 

First, you'll want to check whether ruby is installed. From the shell prompt (denoted here by "%", so don't type the %), type

% ruby -v

(-v tells the interpreter to print the version of ruby), then press the Enter key. If ruby is installed, you will see a message something like the following:

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

If ruby is not installed, you can ask your administrator to install it, or you can do it yourself , since ruby is free software with no restrictions on its installation or use.

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

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

More conventionally, a ruby program can be stored in a file.

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

^D is control-D. The above is just for UNIX. If you're using DOS, try this:

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

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

Some surprisingly complex and useful things can be done with miniature programs that fit in a command line. For example, this one replaces foo with bar in all C source and header files in the current working directory, backing up the original files with ".bak" appended:

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

This program works like the UNIX cat command (but works slower than cat):

% ruby -pe 0 file


What is ruby?
Prev
Contents
Contents
Simple examples
Next

 
 
  Published under the terms of the OpenLDAP Public License Design by Interspire