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
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.
| 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" 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
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
This will produce the following result:
初始化 Ruby 程序 这是主 Ruby 程序
Ruby ENDStatement
Syntax
END {
code
}
Declarationcodewill be called at the end of the program.
Examples
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:
Alternatively, comments can follow a statement or expression on the same line after it:
You can comment multiple lines as shown below:
Here is another form. This block comment hides lines between =begin/=end from the interpreter: