Easy Tutorial
❮ Ruby Xml Xslt Xpath Ruby Exceptions ❯

Ruby Syntax

Let's write a simple Ruby program. All Ruby files have the extension .rb. So, place the following source code in a file named test.rb.

Example

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

Here, it is assumed that you have a Ruby interpreter available in your /usr/bin directory. Now, try running this program as follows:

$ ruby test.rb

This will produce the following result:

Hello, Ruby!

You have seen a simple Ruby program. Now, let's look at some basic concepts related to Ruby syntax:

Whitespace in Ruby Programs

Whitespace characters, such as spaces and tabs, are generally ignored in Ruby code unless they appear within a string. However, they are sometimes used to interpret ambiguous statements. When the -w option is enabled, this interpretation can produce warnings.

Example:

a + b is interpreted as a+b (this is a local variable)
a  +b is interpreted as a(+b) (this is a method call)

End of Line in Ruby Programs

Ruby interprets semicolons and newline characters as the end of a statement. However, if Ruby encounters an operator such as +, -, or a backslash at the end of a line, it indicates the continuation of a statement.

Ruby Identifiers

Identifiers are names for variables, constants, and methods. Ruby identifiers are case-sensitive. This means that Ram and RAM are two different identifiers in Ruby.

The names of Ruby identifiers can include letters, digits, and underscore characters (_).

Reserved Words

The following table lists the reserved words in Ruby. These reserved words cannot be used as names for constants or variables. However, they can be used as method names.

| BEGIN | do | next | then | | END | else | nil | true | | alias | elsif | not | undef | | and | end | or | unless | | begin | ensure | redo | until | | break | false | rescue | when | | case | for | retry | while | | class | if | return | yield | | def | in | self | __FILE__ | | defined? | module | super | __LINE__ |

Here Document in Ruby

A "Here Document" refers to creating multi-line strings. After <<, you can specify a string or an identifier to terminate the string, and all lines following the current line up to the terminator are the value of the string.

If the terminator is enclosed in quotes, the type of quotes determines the type of the line-oriented string. Note that there must be no space between << and the terminator.

Here are different examples:

Example

#!/usr/bin/ruby -w
# -*- coding : utf-8 -*-

print <&lt;EOF
    This is the first way to create a here document.
    Multi-line string.
EOF

print <<"EOF";                # Same as above
    This is the second way to create a here document.
    Multi-line string.
EOF

print <<`EOC`                 # Execute commands
    echo hi there
    echo lo there
EOC

print <<"foo", <<"bar"          # You can stack them
    I said foo.
foo
    I said bar.
bar

This will produce the following result:

This is the first way to create a here document.
Multi-line string.
This is the second way to create a here document.
Multi-line string.
hi there
lo there
I said foo.
I said bar.

Ruby BEGIN Statement

Syntax

BEGIN {
   code
}

Declares that code will be called before the program runs.

Example

#!/usr/bin/ruby

puts "This is the main Ruby program"

BEGIN {
   puts "Initializing the Ruby program"
}

This will produce the following result:

Initializing the Ruby program
This is the main Ruby program

Ruby END Statement

Syntax

END {
   code
}

Declares that code will be called at the end of the program.

Example

#!/usr/bin/ruby

puts "This is the main Ruby program"

END {
   puts "Stopping the Ruby program"
}
BEGIN {
   puts "Initializing the Ruby program"
}

This will produce the following result:

Initializing the Ruby program
This is the main Ruby program
Stopping the Ruby program

Ruby Comments

Comments hide a line, part of a line, or several lines from the Ruby interpreter. You can use the hash character (#) at the beginning of a line:

# I am a comment, please ignore me.

Or, a comment can follow the same line as a statement or expression:

name = "Madisetti" # This is also a comment

You can comment multiple lines as follows:

# This is a comment.
# This is also a comment.
# This is also a comment.
# This is still a comment.

Here is another form. This block comment hides lines between =begin/=end from the interpreter:

=begin
This is a comment.
This is also a comment.
This is also a comment.
This is still a comment.
=end
❮ Ruby Xml Xslt Xpath Ruby Exceptions ❯