GNU/Linux Programmer's Solver - Ruby

Hello World program in Ruby. Basic structure, comments and string literals
PROGRAMMING RUBY HELLO WORLD
#!/usr/bin/ruby -w

# This is a comment
puts ("Hello World!")

=begin
This is a comment
using multiple lines
=end
print <<EOM
-----------------
This is a character strings
using multiple lines
-----------------
EOM

print %{
-----------------
This is other character string
using multiple lines. Needs Ruby 1.99 or
higher
-----------------
}

Ruby is another relatively popular scripting language. Personally, I find it very elegant, although it’s not as widespread as Perl or Python.

Programs in Ruby use the .rb extension.

We can see a lot of similarities to both Perl and Python. Comments with # or using special blocks (in this case =begin/=end) and here-doc like in Perl, parentheses in function calls are optional. And the ends of instructions are indicated with a new line, like in Python. As in all the languages we have seen so far, strings are represented between quotes.

In this program, we used puts to print the first string. This function behaves exactly like its namesake in C, automatically adding a carriage return.

Ruby offers several options for defining multi-line strings. The example program shows some examples. Special mention requires the last example. The % character allows us to define different types of literals, depending on the letter that follows. In this case, no letter follows and therefore it defines a string.

DID YOU KNOW…

We can use different separators to define literals with %. In the example program we used { and }, but we can also use (, [, in which case we will have to delimit the end of the literal with the characters ) or ]. But in general we could use other characters like ~, %, |, ^, … In these cases, the same character is used to indicate the end of the literal.

There’s not much more to say about this program. Interesting features of Ruby will be seen later.

To run the program, as with all our interpreted languages, we give permissions to the file and use the interpreter in case we use the she-bang.

$ ruby hello.rb
$ chmod +x hello.rb
$ ./hello

Ruby was designed by Yukihiro “Matz” Matsumoto in Japan. According to Matsumoto, the language was conceived in 1993 as a purely object-oriented language, something the author hadn’t found in other languages like Perl or Python. The author describes it as a simple Lisp with object-orientation inspired by Smalltalk, blocks inspired by higher-order functions, and the practical utility of Perl.

The first public version of Ruby was its 0.95 version in 1995. After that, three new versions were released in the two days following.

DID YOU KNOW…

With the exception of versions 1.8 and 2.0, the rest of Ruby versions are released on Christmas Day…. Quite a gift. Apparently, since Ruby was Matsumoto’s personal project, the versions were released during vacation periods when he had more time.

Summary

  • Interpreted language
  • Extension: rb
  • Line terminator: Carriage return
  • Groups instructions with: Depends on the command
  • Multi-line comments: =begin/=end
  • Single-line comments: #
  • String delimiter: "string" or 'string'
  • You can print text with puts (adds carriage return)
  • Supports multi-line strings %{..}
  • Supports HERE-DOCS
  • Entry point: Beginning of the file

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Python

Hello World program in Python. Basic structure, comments and string literals

Read

PROGRAMMING PYTHON HELLO WORLD

GNU/Linux Programmer's Solver - Bash

Hello World program in Bash. Basic structure, comments and string literals

Read

PROGRAMMING BASH HELLO WORLD SHELL PROGRAMMING

Return to Home Page