Ruby Syntax

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

Examples

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

Run Example »

Here, assume you have a usable Ruby interpreter in your /usr/bin directory. Now, try running this program as shown below:

$ 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 in Ruby code, such as spaces and tabs, are generally ignored unless they appear in strings. However, sometimes they are used to interpret ambiguous statements. When the -w option is enabled, such interpretations can generate warnings.

Examples:

a + b 被解释为 a+b (这是一个局部变量)
a  +b 被解释为 a(+b) (这是一个方法调用)

Line Endings in Ruby Programs

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

Ruby Identifiers

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

Ruby identifier names can contain letters, numbers, 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.

BEGINdonextthen
ENDelseniltrue
aliaselsifnotundef
andendorunless
beginensureredountil
breakfalserescuewhen
caseforretrywhile
classifreturnyield
definself__FILE__
defined?modulesuper__LINE__

Here Document in Ruby

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

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

Here are different examples:

Examples

#!/usr/bin/ruby -w # -*- coding : utf-8 -*- print <<EOFThis is the first way to createhere document.\n multi-line string.EOF print <<"EOF"; #Same as aboveThis is the second way to createhere document.\n multi-line string.EOF print <<`EOC` #Execute command echo hi there echo lo there EOC print <<"foo", <<"bar" #You can stack them I said foo. foo I said bar. bar

Run Example »

This will produce the following result:

这是第一种方式创建here document 。
多行字符串。
这是第二种方式创建here document 。
多行字符串。
hi there
lo there
I said foo.
I said bar.

Ruby BEGINStatement

Syntax

BEGIN {
   code
}

Declarationcodewill be called before the program runs.

Examples

#!/usr/bin/ruby puts "This is the main Ruby program" BEGIN { puts "Initializing Ruby program" }

This will produce the following result:

初始化 Ruby 程序
这是主 Ruby 程序

Ruby ENDStatement

Syntax

END {
   code
}

Declarationcodewill be called at the end of the program.

Examples

#!/usr/bin/ruby puts "This is the main Ruby program" END { puts "Stopping Ruby program" } BEGIN { puts "Initializing Ruby program" }

This will produce the following result:

初始化 Ruby 程序
这是主 Ruby 程序
停止 Ruby 程序

Ruby Comments

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

#I am a comment, please ignore me.

Alternatively, comments can follow a statement or expression on the same line after it:

name = "Madisetti" #This is also a comment

You can comment multiple lines as shown below:

#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:

=beginThis is a comment.\nThis is also a comment.\nThis is also a comment.\nThis is still a comment.=end
Other extensions