My First Ruby Program

Let's start our Ruby editor SciTE. To do so, on your windows desktop click on start/Programs/Ruby-186-25/SciTE. The editor window opens. Press the F8 key to open an output window. Now, click on Options/Open Global Options File and search for 'tabsize'. Edit and make tabsize=2 and indent.size=2. I like my SciTE window to be maximized at start and for that set the position.width=-1 and position.height=-1 Press Ctrl+S and the Ctrl+W. Next, press Ctrl+Shift+I - this opens the Indentation Settings window. Here, ensure that the Tab Size and Indent Size is set to 2 and that the Use tabs box is not checked. Click OK. We are now ready to write our first Ruby program.

Create a folder named, say. rubyprograms on your C:/ We shall store all our programs in this folder. Our first program will display the string 'Hello' on the command window and the name of the program will be p001hello.rb

By convention, Ruby source files have the .rb file extension. In Microsoft Windows, Ruby source files sometimes end with .rbw, as in myscript.rbw. The Ruby coding convention states that file/directory name is lower case of class/module name with .rb extension. For example, Foo class has name foo.rb

Code layout is pretty much up to you; indentation is not significant (but using two-character indentation will make you friends in the community if you plan on distributing your code).

In the left window of SciTE type: puts 'Hello' and then click File/Save As... Give the name p001hello.rb and store it in your rubyprograms folder. Press F5 to run your program. You should see Hello in the output window on the right. The program is as shown below:

Note: Ruby is a scripting language. There is no special main method in Ruby from which execution begins. The Ruby interpreter is given a script of statements to execute, and it begins executing at the first line and continues to the last line. puts (s in puts stands for string; puts really means put string) simply writes onto the screen whatever comes after it, but then it also automatically goes to the next line.

a. Parentheses are usually optional with a method call. These calls are all valid:
foobar
foobar()
foobar(a, b, c)
foobar a, b, c

b. In Ruby, everything from an integer to a string is considered to be an object (more on this later). And each object has built in 'methods' (Ruby term for functions) which can be used to do various useful things. To use a method, you need to put a dot after the object, and then append the method name. Some methods such as puts and gets are available everywhere and don't need to be associated with a specific object.
Technically speaking, these methods are provided by Ruby's Kernel module (more on this later) and they are included in all Ruby objects (the Kernel module is included by class (more on this later) Object, so its methods are available in every Ruby object). When you run a Ruby application, an object called main of class Object is automatically created. This object provides access to the Kernel methods.

Observe:

  1. Java and C programmers - no need to write a main method/function
  2. String literals are sequences of characters between single or double quotation marks. I am using single quotes around Hello. ' is more efficient than " (more on this later)
  3. Ruby is an interpreted language, so you don't have to recompile to execute the program written in Ruby
  4. Ruby releases with even subversion numbers - 1.6, 1.8, and so on - are stable, public releases. However, in 2007 Matz broke with convention and made 1.9 a stable public release of Ruby.
  5. The Ruby coding convention states that file/directory name is lower case of class/module name with .rb extension. For example, Foo class has name foo.rb