This short tutorial should take no more than twenty minutes of your time. It assumes that you have already installed Ruby; If not, you are warmly advised to do so before continuing your reading.
A first approach of Ruby, the simplest, is to use IRB in a terminal:
- if you are using Mac OS X, open a Terminal and type irb, then press [enter];
- if you are on Linux, open a console and type irb, then press [enter];
- If you are using Windows, launch Interactive Ruby (IRB) in the Ruby section of the Start> Applications menu.
IRB allows you to write and interpret Ruby without having to save a file and then execute it. Here, IRB is launched. And now ?
Type this: "Hello World" and press enter to confirm (you will need to do that after each new line).
What has just happened ? Do we just write the shortest ever Hello World program? Not quite ... but almost. The second line, starting with the sign =>, is how IRB informs us of the result of the last expression evaluated. If we really want to write "Hello World", it takes a little more: do you know the "puts" and "nil" commands?
- "puts" is the basic command to write something with Ruby. And "nil", what IRB presents us, what is it? The result of our phrase "puts" "Hello World", of course ! In fact, "puts" always returns "nil", which is, for Ruby and soon, for you, total lack of value. And so now, Ruby is a little clearer for you ?
Modules, or how to group the code by thematic
What is called in Ruby a module? A module is provided to you automatically (it is part of the standard library, which groups the modules of general interest). The modules have two major roles to play within Ruby. The first of these roles is to group together methods (often referred to as "functions" in other languages). For more information, contact our specialist ruby!