6.9. Ruby syntax

发布时间 :2023-10-24 23:00:07 UTC      

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.

6.9.1. 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:

6.9.2. 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 #

6.9.3. Grammar #

BEGIN {
   code
}

Statement code Will be called before the program runs.

6.9.4. 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 #

6.9.5. Grammar #

END {
   code
}

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

6.9.6. 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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.