Difference between revisions of "Ruby2"
Gonzalo58T (Talk | contribs) |
Gonzalo58T (Talk | contribs) (→Your first program) |
||
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
== Development environment == | == 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. | ||
+ | |||
+ | === Runtime === | ||
+ | |||
+ | The runtime can be downloaded on [http://www.ruby-lang.org/en/downloads/ 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 === | ||
+ | |||
+ | 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 [http://docs.rubygems.org/read/chapter/3/ this page] for instruction on how to set it up. | ||
+ | |||
+ | It comes packaged in most Unix OSes' package repositories. | ||
== Your first program == | == Your first program == | ||
=== Code === | === Code === | ||
+ | (will work under Unix-like OSes only) | ||
+ | <code> | ||
+ | cat > hello-world.rb <<EOF | ||
+ | #!/usr/bin/env ruby1.9 | ||
+ | puts "Hello, world" | ||
+ | EOF | ||
+ | </code> | ||
+ | Run by calling | ||
+ | <code> | ||
+ | chmod +x hello-world.rb | ||
+ | ./hello-world.rb | ||
+ | </code> | ||
=== Analysis === | === Analysis === | ||
+ | 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 == | == Variables & Data Types == |
Latest revision as of 15:03, 16 August 2012
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.
Contents
Basics
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.
Runtime
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
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
Code
(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
./hello-world.rb
Analysis
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
Scalars
Non-scalars
A bit of magic: Blocks & Lambdas
Object-oriented programming
Classes
Defining a class
Inheritance
Scope
Objects
Operations and control structures
Assignation, mathematical operations
String & Array manipulation
Boolean operations
AND & OR
NOT
Control structures
if, unless
while, until
loop control
I/O
Input
User input
Streams input
Output
File output
Storage
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 1.3.2.1 Helper Functions 1.3.2.1.1 join() 1.3.2.1.2 split() 1.3.2.1.3 push() 1.3.2.1.4 pop() 1.3.2.1.5 unshift() 1.3.2.1.6 shift() 1.3.3 Hashes 1.3.3.1 Introduction 1.3.3.2 Helper Functions 1.3.3.2.1 each() 1.3.3.2.2 keys 1.3.4 References 1.3.4.1 Hash References 1.3.4.2 Callback References 1.3.5 Casting 1.4 Boolean Logic 1.4.1 Operators 1.4.1.1 Mathematical 1.4.1.2 Regular Expressions 1.4.2 Statements 1.4.2.1 if 1.4.2.2 unless 1.4.2.3 AND and OR 1.4.2.4 switch 1.4.2.5 Golfing 1.4.3 Helper Natives 1.4.3.1 exists 1.4.3.2 defined 1.4.3.3 undef 1.4.4 Bitwise Manipulations 1.4.4.1 AND 1.4.4.2 NOT 1.4.4.3 OR 1.4.4.4 XOR 1.4.4.5 Bit Shifting 1.4.4.6 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 1.6.1.1 Getopt::Std 1.6.1.1.1 Code 1.6.1.1.2 Analysis 1.6.1.2 Getopt::Long 1.6.1.2.1 Code 1.6.1.2.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 2.1.2.1 Config.pm 2.1.2.2 Log.pm 2.1.2.3 Server.pm