Questions about this topic? Sign up to ask in the talk tab.


From NetSec
Jump to: navigation, search

Ruby is an object-oriented interpreted language. Several interpreters exist, the main one being written in C. It natively supports threads, fibers and has an impressive amount of third-party libraries that enable it to cover a broad spectrum of requirements, from basic file processing to complex distributed computing servers.


Development environment

To start developing in Ruby, you will need a text editor and the Ruby interpreter. Later on you may be interested by extra packages provided by RubyGems. It is recommended to develop and run Ruby under a Unix-style OS, but it's also perfectly compatible with Windows.

Ruby has two parallel branches as of today (August 2012): Ruby 1.8 and Ruby 1.9. They are incompatible, and it is recommended to run Ruby 1.9, as it is faster, better supported and the future of Ruby.

Text Editors On Windows

  • Notepad++
  • SciTE
  • GVim

Text Editors under Unix-like OSes

If you use a Unix-like OS, you're grown enough to know your preference. Ruby has syntax highlighting support for both emacs and vim. Gedit also supports it.


The runtime can be downloaded on this page. It is a command-line application so it will need a Terminal (cmd.exe under Windows, xterm or whatever you prefer under linux) to host your program.

Ruby comes packaged with most Linux distributions. Make a search for it and be sure to select the 1.9 or later branch.


Rubygems is the equivalent to Perl's CPAN. It will be very useful as you dive deeper in Ruby development because of the very large set of libraries made available through it.

See this page for instruction on how to set it up.

It comes packaged in most Unix OSes' package repositories.

Your first program


(will work under Unix-like OSes only) cat > hello-world.rb <<EOF

  1. !/usr/bin/env ruby1.9

puts "Hello, world" EOF

Run by calling chmod +x hello-world.rb ./hello-world.rb


In Ruby, there is no entrypoint: all the statements are executed in order. Hence, Ruby reads the file and executes the statement it meets, puts "Hello, world". puts prints a string given in parameter, followed by a newline.

You can notice that there is no parenthesis around the argument: in Ruby, these are optional.

Variables & Data Types



A bit of magic: Blocks & Lambdas

Object-oriented programming


Defining a class




Operations and control structures

Assignation, mathematical operations

String & Array manipulation

Boolean operations



Control structures

if, unless

while, until

loop control



User input

Streams input


File output


1 Basics 1.1 Development Environment 1.1.1 Linux & Unix 1.1.2 Windows 1.1.3 CPAN 1.2 Your first program 1.2.1 Code 1.2.2 Analysis 1.3 Variables & Data Types 1.3.1 Scalars 1.3.2 Arrays Helper Functions join() split() push() pop() unshift() shift() 1.3.3 Hashes Introduction Helper Functions each() keys 1.3.4 References Hash References Callback References 1.3.5 Casting 1.4 Boolean Logic 1.4.1 Operators Mathematical Regular Expressions 1.4.2 Statements if unless AND and OR switch Golfing 1.4.3 Helper Natives exists defined undef 1.4.4 Bitwise Manipulations AND NOT OR XOR Bit Shifting Bit Rotation 1.5 Loops 1.5.1 While 1.5.2 Until 1.5.3 For 1.5.4 Foreach 1.6 User Input 1.6.1 Command Line Arguments Getopt::Std Code Analysis Getopt::Long Code Analysis 1.6.2 STDIN (Standard Input) 1.7 User-Defined Functions 2 Helpful Libraries 2.1 Throughput 2.1.1 Download 2.1.2 Usage