Ruby syntax


Release date:2023-10-25 Update date:2023-10-25 Editor:admin View counts:226

Label:

Ruby syntax

Let’s write a simple Ruby program. All Ruby file extensions are .rb . so, put the following source code in test.rb in the file.

Example

#!/usr/bin/ruby -wputs"Hello, Ruby!";

Here, assume that your /usr/bin Ruby interpreter is already availablein the directory. Now, try to run the program, as follows:

$ ruby test.rb

This will produce the following results:

Hello, Ruby!

Now that you’ve seen a simple Ruby program, let’s look at some basic concepts related to Ruby syntax:

Blanks in Ruby programs

White space characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in a string. However, sometimes they are used to explain ambiguous sentences. When enabled -w option, this interpretation generates a warning.

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)

The end of a line in a Ruby program

Ruby interprets semicolons and newline characters as the end of a statement.However, if Ruby encounters operators at the end of the line, such as +, -,or backslash, they represent the continuation of a statement.

Ruby identifier

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

The name of an Ruby identifier can contain letters, numbers, and underscore characters ( _ ).

Reserved word

The following table lists the reserved words in Ruby. These reserved words cannot be used as the names of 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

“Here Document” means to create a multiline string. After <<, you can specify a string or identifier to terminate the string, and all lines after the current line until the Terminator are the values of the string.

If the Terminator is enclosed in quotation marks, the type of quotation marks determines the type of 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<<EOF This is the first way to create a heredocument.
Multiple line strings. EOFprint<<"EOF"# Same as above, this is the second way to create a heredocument.
Multiple line strings. EOFprint<< ` EOC ` # Execute the command echoitherecholothereEOCprint<"foo",
<<"bar" # You can stack them Isaidfoo.fooIsaidbar.bar

This will produce the following results:

This is the first way to create a here document.
Multiple line strings.
This is the second way to create a here document.
Multiple line strings.
Hi there
Lo there
I said foo
I say bar

Ruby BEGIN statement

Grammar

BEGIN {
   code
}

Statement code Will be called before the program runs.

Example

#/ Usr/bin/rubyputs "This is the main Ruby program"
BEGIN {puts "Initialize Ruby program"}

This will produce the following results:

Initialize Ruby program
This is the main Ruby program

Ruby END statement

Grammar

END {
   code
}

Statement code will be called at the end of the program.

Example

#/ Usr/bin/rubyputs "This is the main Ruby program" END {puts "to stop Ruby
Program '{BEGIN {puts' initializes Ruby programs'}

This will produce the following results:

Initialize Ruby program
This is the main Ruby program
Stop Ruby program

Ruby comment

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

#I am a comment, please ignore me.

Alternatively, comments can follow the same line of a statement or expression:

name="Madisetti"#This is also a comment

You can comment on 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 kind of block comment is hidden from the interpreter =begin/=end the line between:

=Begin, this is the annotation. This is also a comment.
This is also a comment. This is still a comment= End

Powered by TorCMS (https://github.com/bukun/TorCMS).