My First Ruby Program
Let's open up our plain-text editor. As far as possible, ensure that your editor's Tab is set to 2 spaces. We are now ready to write our first Ruby program.
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).
Create a folder named, say. rubyprograms. 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 say 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
Type the following in your editor:
and then click File/Save As... Give the name p001hello.rb and store it in your rubyprograms folder. To run your program, open a command window and type ruby p001hello.rb as shown below:
You should see the output as shown above.
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(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.
- Java and C programmers - no need to write a main method/function
- String literals are sequences of characters between single or double quotation marks.
- Ruby is an interpreted language, so you don't have to recompile to execute the program written in Ruby
- 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
Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on rubylearning.com and in the course at rubylearning.org is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.