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 188.8.131.52 Helper Functions 184.108.40.206.1 join() 220.127.116.11.2 split() 18.104.22.168.3 push() 22.214.171.124.4 pop() 126.96.36.199.5 unshift() 188.8.131.52.6 shift() 1.3.3 Hashes 184.108.40.206 Introduction 220.127.116.11 Helper Functions 18.104.22.168.1 each() 22.214.171.124.2 keys 1.3.4 References 126.96.36.199 Hash References 188.8.131.52 Callback References 1.3.5 Casting 1.4 Boolean Logic 1.4.1 Operators 184.108.40.206 Mathematical 220.127.116.11 Regular Expressions 1.4.2 Statements 18.104.22.168 if 22.214.171.124 unless 126.96.36.199 AND and OR 188.8.131.52 switch 184.108.40.206 Golfing 1.4.3 Helper Natives 220.127.116.11 exists 18.104.22.168 defined 22.214.171.124 undef 1.4.4 Bitwise Manipulations 126.96.36.199 AND 188.8.131.52 NOT 184.108.40.206 OR 220.127.116.11 XOR 18.104.22.168 Bit Shifting 22.214.171.124 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 126.96.36.199 Getopt::Std 188.8.131.52.1 Code 184.108.40.206.2 Analysis 220.127.116.11 Getopt::Long 18.104.22.168.1 Code 22.214.171.124.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 126.96.36.199 Config.pm 188.8.131.52 Log.pm 184.108.40.206 Server.pm