6.9. Ruby syntax

发布时间 : 2025-10-25 13:34:02 UTC      

Page Views: 9 views

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<   

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。