2.2. Hello World

Ruby is a scripting language. So it is not compiled and then executed, but read by an interpreter and then processed line by line.
A simple Ruby program hello-world.rb consist of the following line:
puts 'Hello World!'
Use your favorite editor to open a new file with the filename hello-world.rb and insert the above line into it. You can then execute this Ruby program in the command line as follows:
$ ruby hello-world.rb 
Hello World!
$ 
A program line in a Ruby program does not have to end with a semicolon. The Ruby interpreter is even so intelligent that is recognizes if a program line was split over two or more lines for the sake of readability. I will spare you the corresponding examples and am only mentioning this so you don't say or think later, "is it okay like this?"
Indenting code is also not necessary. But it does make it much easier to read for human beings!

puts and print

If you go looking for examples on Ruby on the Internet, you will find two typical ways of printing text on the screen:
  • puts
    Prints a string, followed by a newline.
  • print
    Prints a string (without newline). If the string itself contains a newline, this will be printed as normal.
Example program (an extension of the program hello-world.rb):
puts 'Hello World!'
puts
puts '------------'
print 'Hello World!'
print
puts '------------'
On the screen, you will see this:
$ ruby hello-world.rb 
Hello World!

------------
Hello World!------------
$ 

Comments

A comment in a Ruby program starts with a #-sign and ends with a newline. As an example, I can add a comment to the hello-world.rb above:
# Program for displaying "Hello World!"
# by Stefan Wintermeyer

puts 'Hello World!'
A comment can also follow a program line:
# Program for displaying "Hello World!"
# by Stefan Wintermeyer

puts 'Hello World!'  # output
A #-sign within strings in inverted commas is not treated as the start of a comment. Example program:
# Example program
# by Stefan Wintermeyer

puts 'Hello World!'
puts '############'
puts
puts '1#2#3#4#5#6#'  # Comment on this
On the screen, you will see this:
$ ruby hello-world.rb 
Hello World!
############

1#2#3#4#5#6#
$

Help via ri

When programming, you do not always have a Ruby handbook available. Fortunately, the Ruby developers thought of this and provided a built-in help feature in form of the program ri (of course only if you have installed the documentation). ri is the abbreviation for Ruby Index. The output gets opend in a so called pager (z.B. less). The parameter -T sets the output directly to stdout.
This is a typical chicken and egg situation. How can I explain the Ruby help feature, if we are only just getting started with Ruby? So I am going to jump ahead a little and show you how you can search for information on the class String:
$ ri String
String < Object

------------------------------------------------------------------------------
Includes:
Comparable (from ruby site)

(from ruby site)
------------------------------------------------------------------------------


Rake extension methods for String.

A String object holds and manipulates an arbitrary sequence of bytes,
typically representing characters. String objects may be created using
String::new or as literals.

Because of aliasing issues, users of strings should be aware of the methods
that modify the contents of a String object.  Typically, methods with
names ending in ``!'' modify their receiver, while those without a ``!''
return a new String.  However, there are exceptions, such as
String#[]=.
------------------------------------------------------------------------------
[...]
$
If we are looking for information on a specific method (chicken-egg!), then we can also use ri. Let's take gsub as an example. This is a method for replacing parts of a String (that is useful now and again).
$ ri String.size
= String.size

(from ruby site)
------------------------------------------------------------------------------
  str.size     -> integer

------------------------------------------------------------------------------

Returns the character length of str.

$ 
The program ri always prints the output in the pager program defined by the shell (for example less). You can also use the command option -T to output everything directly to STDOUT. In the book I am going to be using both variations.

Thank you for your support and the visibility by linking to this website on Twitter and Facebook. That helps a lot!