Ruby Tutorial
Ruby Tutorial
Audience
This tutorial has been prepared for beginners to help them understand the basic to
advanced concepts related to Ruby Scripting languages.
Prerequisites
Before you start practicing with various types of examples given in this tutorial, we are
making an assumption that you are already aware of computer programs and
programming languages in general.
All the content and graphics published in this e-book are the property of Tutorials Point
(I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or
republish any contents or a part of contents of this e-book in any manner without written
consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely
as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I)
Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of
our website or its contents including this tutorial. If you discover any errors on our
website or in this tutorial, please notify us at [email protected]
i
Table of Contents
About the Tutorial ......................................................................................................................................
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
ii
Reserved Words ...................................................................................................................................... 12
Ruby Comments...................................................................................................................................... 15
Floating Numbers.................................................................................................................................... 29
iii
Ruby Ranges ........................................................................................................................................... 32
iv
Ruby until modifier ................................................................................................................................. 52
Class Methods......................................................................................................................................... 61
v
Character Encoding ................................................................................................................................. 75
vi
19. RUBY – FILE I/O ................................................................................................................. 123
vii
Using retry Statement .......................................................................................................................... 143
viii
Regular-Expression Examples ................................................................................................................ 171
Anchors................................................................................................................................................. 173
ix
Form Processing .................................................................................................................................... 200
TkCheckButton...................................................................................................................................... 256
TkListbox............................................................................................................................................... 265
TkComboBox......................................................................................................................................... 272
xi
TkText ................................................................................................................................................... 305
xii
Further Reading .................................................................................................................................... 355
RubyGems............................................................................................................................................. 390
xiii
Ruby Profiler ......................................................................................................................................... 401
xiv
Ruby
1. RUBY – OVERVIEW
You can find the name Yukihiro Matsumoto on the Ruby mailing list at www.ruby-
lang.org. Matsumoto is also known as Matz in the Ruby community.
Ruby has features that are similar to those of Smalltalk, Perl, and Python. Perl, Python,
and Smalltalk are scripting languages. Smalltalk is a true object-oriented language.
Ruby, like Smalltalk, is a perfect object-oriented language. Using Ruby syntax is much
easier than using Smalltalk syntax.
Features of Ruby
Ruby is an open-source and is freely available on the Web, but it is subject to a
license.
Ruby has a clean and easy syntax that allows a new developer to learn very
quickly and easily.
Ruby has similar syntax to that of many programming languages such as C++
and Perl.
Ruby is very much scalable and big programs written in Ruby are easily
maintainable.
Ruby support many GUI tools such as Tcl/Tk, GTK, and OpenGL.
Ruby has a rich set of built-in functions, which can be used directly into Ruby
scripts.
1
Ruby
Ruby 1.8.5
This tutorial will provide the necessary skills to create GUI, networking, and Web
applications using Ruby. It also will talk about extending and embedding Ruby
applications.
What is Next?
The next chapter guides you to where you can obtain Ruby and its documentation.
Finally, it instructs you on how to install Ruby and prepare an environment to develop
Ruby applications.
2
Ruby
2. RUBY – ENVIRONMENT SETUP
Try the following example using the Try it option available on our website at the top
right corner of the sample code box given below:
#!/usr/bin/ruby -w
For most of the examples given in this tutorial, you will find a Try it option on our
website code sections at the top right corner that will take you to the online compiler. So
just make use of it and enjoy your learning.
Ruby Command Line Options : This chapter list out all the command line
options, which you can use along with Ruby interpreter.
Ruby Environment Variables : This chapter has a list of all the important
environment variables to be set to make Ruby Interpreter works.
Download a zipped file having latest version of Ruby. Follow Download Link.
3
Ruby
After having downloaded the Ruby archive, unpack it and change into the newly
created directory:
$ ./configure
$ make
After installation, make sure everything is working fine by issuing the following
command on the command-line:
$ruby -v
ruby 1.6.7 (2002-06-04) [i386-netbsd]
If everything is fine, this should output the version of the installed Ruby
interpreter as shown above. You may have installed different version, so it will
display a different version.
NOTE: You may have different versions available at the time of installation.
Download a zipped file having latest version of Ruby. Follow Download Link.
After having downloaded the Ruby archive, unpack it and change into the newly
created directory:
4
Ruby
Click Next to move to the Important Information page of the wizard and keep
moving till Ruby installer completes installing Ruby.
You may need to set some environment variables if your installation has not setup them
appropriately.
If you use Windows 9x, add the following lines to your c:\autoexec.bat: set
PATH="D:\(ruby install directory)\bin;%PATH%"
o Add your Ruby directory to the end of the Variable Value list and click OK.
o Add .RB and .RBW to the Variable Value list and click OK.
After installation, make sure everything is working fine by issuing the following
command on the command-line:
$ruby -v
ruby 1.6.7
If everything is fine, this should output the version of the installed Ruby
interpreter as shown above. You may have installed different version, so it will
display a different version.
The interpreter can be invoked with any of the following options to control the
environment and behavior of the interpreter.
Option Description
5
Ruby
-F pat Specifies pat as the default separator pattern ($;) used by split.
-e prog Specifies prog as the program from the command line. Specify multiple
-e options for multiline programs.
-i [ ext] Overwrites the file contents with program output. The original file is
saved with the extension ext. If ext isn't specified, the original file is
deleted.
-K [ kcode] Specifies the multibyte character set code (e or E for EUC (extended
Unix code); s or S for SJIS (Shift-JIS); u or U for UTF-8; and a, A, n, or
N for ASCII).
-n Places code within an input loop (as in while gets; ... end).
-0[ octal] Sets default record separator ($/) as an octal. Defaults to \0 if octal not
specified.
-T [level] Sets the level for tainting checks (1 if level not specified).
-w Enables verbose mode. If program file not specified, reads from STDIN.
-x [dir] Strips text before #!ruby line. Changes directory to dir before
executing if dir is specified.
6
Ruby
Single character command-line options can be combined. The following two lines express
the same meaning:
Variable Description
7
Ruby
RUBYLIB Search path for libraries. Separate each path with a colon
(semicolon in DOS and Windows).
For Unix, use env command to see a list of all the environment variables.
HOSTNAME=ip-72-167-112-17.ip.secureserver.net
RUBYPATH=/usr/bin
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=122.169.131.179 1742 22
SSH_TTY=/dev/pts/1
USER=amrood
JRE_HOME=/usr/java/jdk/jre
J2RE_HOME=/usr/java/jdk/jre
PATH=/usr/local/bin:/bin:/usr/bin:/home/guest/bin
MAIL=/var/spool/mail/guest
PWD=/home/amrood
INPUTRC=/etc/inputrc
8
Ruby
JAVA_HOME=/usr/java/jdk
LANG=C
HOME=/root
SHLVL=2
JDK_HOME=/usr/java/jdk
LOGDIR=/usr/log/ruby
LOGNAME=amrood
SSH_CONNECTION=122.169.131.179 1742 72.167.112.17 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
RUBYLIB=/usr/lib/ruby
G_BROKEN_FILENAMES=1
_=/bin/env
If you are working on Windows machine, then you can use any simple text editor
like Notepad or Edit plus.
VIM (Vi IMproved) is a very simple text editor. This is available on almost all Unix
machines and now Windows as well. Otherwise, your can use your favorite vi
editor to write Ruby programs.
Ruby Development Environment (RDE) is also a very good IDE for windows
users.
This tool comes along with Ruby installation so you have nothing to do extra to have IRb
working.
Just type irb at your command prompt and an Interactive Ruby Session will start as
given below:
$irb
irb 0.6.1(99/09/16)
irb(main):001:0> def hello
irb(main):002:1> out = "Hello World"
irb(main):003:1> puts out
9
Ruby
irb(main):004:1> end
nil
irb(main):005:0> hello
Hello World
nil
irb(main):006:0>
Do not worry about what we did here. You will learn all these steps in subsequent
chapters.
What is Next?
We assume now you have a working Ruby Environment and you are ready to write the
first Ruby Program. The next chapter will teach you how to write Ruby programs.
10
Ruby
11