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.
- 1 Basics
- 1.1 Development environment
- 1.2 Your first program
- 1.3 Variables & Data Types
- 1.4 Object-oriented programming
- 1.5 Operations and control structures
- 1.6 I/O
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
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
- !/usr/bin/env ruby1.9
puts "Hello, world" EOF
Run by calling
chmod +x 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
Defining a class
Operations and control structures
Assignation, mathematical operations
String & Array manipulation
AND & OR
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 126.96.36.199 Helper Functions 188.8.131.52.1 join() 184.108.40.206.2 split() 220.127.116.11.3 push() 18.104.22.168.4 pop() 22.214.171.124.5 unshift() 126.96.36.199.6 shift() 1.3.3 Hashes 188.8.131.52 Introduction 184.108.40.206 Helper Functions 220.127.116.11.1 each() 18.104.22.168.2 keys 1.3.4 References 22.214.171.124 Hash References 126.96.36.199 Callback References 1.3.5 Casting 1.4 Boolean Logic 1.4.1 Operators 188.8.131.52 Mathematical 184.108.40.206 Regular Expressions 1.4.2 Statements 220.127.116.11 if 18.104.22.168 unless 22.214.171.124 AND and OR 126.96.36.199 switch 188.8.131.52 Golfing 1.4.3 Helper Natives 184.108.40.206 exists 220.127.116.11 defined 18.104.22.168 undef 1.4.4 Bitwise Manipulations 22.214.171.124 AND 126.96.36.199 NOT 188.8.131.52 OR 184.108.40.206 XOR 220.127.116.11 Bit Shifting 18.104.22.168 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 22.214.171.124 Getopt::Std 126.96.36.199.1 Code 188.8.131.52.2 Analysis 184.108.40.206 Getopt::Long 220.127.116.11.1 Code 18.104.22.168.2 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 22.214.171.124 Config.pm 126.96.36.199 Log.pm 188.8.131.52 Server.pm